2016-04-28 7 views
0

mongooseを使用してMEANスタックを使用して簡単な登録フォームを作成しようとしています。ここで/ 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はデータベースに格納されません。市区町村、電子メール、ユーザーID、パスワードは正しく保管されています。

どのようにデータベースにすべてのものを正しく保存できますか?

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

、あなたがreq.body.firstnamereq.body.lastnameを使用し、次の行にある:あなたがreq.body.Fnamereq.body.Lnameを使用する次のコード行で

+0

req.bodyからfirstname、lastnameを正しく取得していることを確認してください。 – Subburaj

+0

あなたはHTMLフォームを投稿できますか、私は何かがOKではないと仮定します –

+0

リクエストで 'Fname'と' Lname'の値を取得していない可能性があります。 req.bodyをログに記録して、それが何を表示するか試してみてください。 –

答えて

1

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スキーマを使用すると、mongooseのModel.createメソッドを使用して、req.bodyに渡すことができます。あなたはどうしたら、あなたのコードの場合

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

  1. があなたのserver.jsファイルにUserモデルを持参(let User = require('./models/dbSchema.js')
  2. 次に、あなたのapp.postは、次のようになります。

関連する問題