TypeError:Dbschema(Mongoose Schema)不是函数

时间:2016-04-26 10:25:48

标签: 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");

在本地主机上运行时,在表单提交时,Firstname和Lastname不存储在数据库中。 City,eamil用户ID和密码正确存储

如何正确存储数据库中的所有内容请帮帮我?

1 个答案:

答案 0 :(得分:1)

models / 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;

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

谢谢!