【问题标题】:Mongoose schema is not Storing all thing in database猫鼬模式不是将所有东西都存储在数据库中
【发布时间】:2016-08-22 16:55:57
【问题描述】:

我正在尝试使用 mongoose 使用 MEAN 堆栈创建一个简单的注册表单。这是我的 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");

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

我怎样才能正确地将所有东西存储在数据库中,请帮助我?

【问题讨论】:

  • 确保你从 req.body 中获取名、姓是否正确?
  • 你能把 HTML 表单贴出来吗,我认为有些东西不合适
  • 问题可能是您没有在请求中获得FnameLname 的值。尝试记录 req.body 并查看它打印的内容。

标签: angularjs node.js mongodb mongoose mean-stack


【解决方案1】:

在以下代码行中,您使用req.body.Fnamereq.body.Lname

schema.Firstname = req.body.Fname;
schema.Lastname = req.body.Lname;

在接下来的几行中你使用req.body.firstnamereq.body.lastname

 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*/ });
    });

如果您的req.body 对象与您的User Schema 具有相同的键,您可以使用猫鼬的Model.create 方法并将其传递给req.body

对于您的代码,您可以执行以下操作:

  1. 将 User 模型放入您的 server.js 文件 (let User = require('./models/dbSchema.js'))
  2. 那么您的app.post 将如下所示:

    app.post('/regi', function(req,res) {
     //pass the .create method the data from the body
      User.create(req.body, (err, savedUser) => {
        if(err) return res.send(err);
        res.send(savedUser);
      });
    });
    

【讨论】:

    猜你喜欢
    • 2019-08-30
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多