angularjs - 类型错误 : Dbschema(Mongoose Schema) is not a function

标签 angularjs node.js mongodb express mongoose-schema

我正在尝试使用 Mongoose 使用 MEAN_Stack 创建一个简单的注册表单。这是我的 models/dbSchema.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var User = new mongoose.Schema({
   FirstName: String,
   LastName:  String,
   City    :  String,
   Email   :  String,
   Userid  :  String,
   Password:  String
});
module.export = mongoose.model('user', User);

这是我的server.js

var express = require('express');
var app =  express();
var bodyParser = require('body-parser');
var jwt = require('jsonwebtoken');

app.use(express.static(__dirname + "/public"));

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/Regis_module');
var Userschema = require('./models/dbSchema'); 

app.post('/regi',function(req,res){
    var schema        =  new Userschema();
    schema.Firstname      =  req.body.Fname;
    schema.Lastname      =  req.body.Lname;
    schema.City       =  req.body.city;
    schema.Email      =  req.body.email;
    schema.Userid     =  req.body.userid;
    schema.Password   =  req.body.password;
     
    schema.save(function(err) {
        if (err)
            res.send(err);

        res.json({ message: 'Record Inserted', Firstname: req.body.firstname, Lastname: req.body.lastname, city:req.body.city, email:req.body.email, 
                   userid:req.body.userid, password :req.body.password /*, fbId : req.body.fbId*/ });
    });        
});
    
app.listen(3000);
console.log("listening to port 3000");

在本地主机上运行时,在提交表单时,名字和姓氏不会存储在数据库中。城市、电子邮件、用户 ID 和密码存储正确。

如何将所有内容正确存储在数据库中?请帮帮我好吗?

最佳答案

模型/dbSchema.js

它是 module.exports 而不是 module.export。也无需编写 var Schema = mongoose.Schema;。您的代码中没有使用 Schema 变量。

server.js

var schema = new Dbschema({
  FirstName  :  req.body.Fname,
  LastName   :  req.body.Lname,
  City       :  req.body.city,
  Email      :  req.body.email,
  Userid     :  req.body.userid,
  Password   :  req.body.password
});

或者,

var schema = new Dbschema();

schema.FirstName  =  req.body.Fname;
schema.LastName   =  req.body.Lname;
schema.City       =  req.body.city;
schema.Email      =  req.body.email;
schema.Userid     =  req.body.userid;
schema.Password   =  req.body.password;

如果您对此理解有任何问题,请告诉我。如果需要,我会向您发送完整的代码片段。

谢谢!

关于angularjs - 类型错误 : Dbschema(Mongoose Schema) is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862201/

相关文章:

angularjs - 如何注入(inject) Controller 使用的服务/工厂?

angularjs - 保持 md 选项卡标题位置固定在顶部

javascript - 返回正在进行的异步调用的 promise

javascript - MongoDB 第一次不会返回正确的数据

javascript - 对 Angular JS Promise 的通知函数进行单元测试时,永远不会调用通知

javascript - 如何在Angularjs中的字符串插值中获得换行符

node.js - 如何在 Node.js 中获得准确的 UTC 纳秒时间戳

javascript - 如何并行遍历目录树?

node.js - 如何在 Mongoose 中自动创建必填字段

regex - Mongoose :使用正则表达式查询全名