我正在使用mongoosejs在我的數據庫中插入數據。我試圖單獨添加save()回調示例: new ModelName.save(saveData),而不是以常規方式添加它。 新ModelName.save(函數(ERR,型號){...}Mongoose回調問題
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var userDetail = mongoose.model('user_details');
//not working
router.post('/', resgisterUser);
function resgisterUser(req, res) {
new userDetail({
fullName: req.body.fullName,
email: req.body.email,
contactNumber: req.body.contactNumber,
password: req.body.password
})
.save(saveUserDetails);
}
function saveUserDetails(err, newUser) {
console.log(newUser)
res.writeHead(200);
res.end();
}
//working code
function resgisterUser(req, res) {
new userDetail({
fullName: req.body.fullName,
email: req.body.email,
contactNumber: req.body.contactNumber,
password: req.body.password
})
.save(function (err, newUser) {
console.log(newUser)
res.writeHead(200);
res.end();
});
}
`
感謝它的工作。 –