[英]save() callback not being invoked on a mongoose schema object
我试图在我的数据库中保存一个json对象。 不会调用save()函数,但是永远不会保存json对象。 帮我找出问题所在。 我想这是猫鼬的连接问题。 这是我的代码。
var config = require('../config');
var user = require('../user');
api.post('/addUser',function(req,res) {
var userID;
//creating a sample user under Model collection User.. so this becomes a document!!
console.log("addition of new user api hit!!");
//sending a query to retrieve the no of users served
MongoClient.connect(dbURL, function (err, db) {
var UserCountCursor = db.collection("ourusers").find({"docName": "userCount"}).limit(1);
UserCountCursor.each(function (err, doc) {
if (err)
console.log("did not get the count");
else
// var countString= JSON.stringify(doc);
//var docJson=JSON.parse(countString);
console.log("the json content is:" + doc.iparkoUserCount);
//increase the user count by 1 in the db.
var incCount = parseInt(doc.iparkoUserCount) + 1;
console.log("no of userrs:" + incCount);
// making an userId
userID = "ipkoID_C" + incCount.toString();
//updating using MOngoClient
db.collection("ourusers").update({"docName": "userCount"}, {$set: {"iparkoUserCount": incCount}});
console.log("the user count in the db has been updated!!");
console.log("generated id for this guy is:" + userID);
if (userID != null) {
console.log("calling the save function");
//closing the mongoclient connection
db.close();
signUpUser(userID);
}
});
});
function signUpUser(userIDD) {
var me = new user({
name: req.body.new_name,
password: req.body.new_pswd,
username: req.body.new_username,
phno: req.body.new_phn,
userId: userIDD
});
console.log("the obj ::" + JSON.stringify(me));
console.log("obj created and ready to be stored");
//connecting to the db using mongoose
mongoose.connect(config.database, function (err) {
if (err)
console.log("The error is :"+err);
else {
console.log("WE ARE CONNECTED USING MONGOOSE");
//saving the sample user document
me.save(function (err) {
console.log("in the save func");
if (err) throw err;
else {
console.log('User saved Successfully!!!!!');
res.json({
'whatStatus': 'user saved in the database!!',
'userID': userIDD
});
mongoose.connection.close();
}
});
}
});
}
});
我的控制台日志::
增加了新的用户api !! json的内容是:143用户数:144数据库中的用户数已更新! 此人生成的ID为:ipkoID_C144,调用保存函数obj :: {“ name”:“ Abhi”,“ password”:“ jio”,“ username”:“ abhijio”,“ phno”:“ 45142545”,“ userId“:” ipkoID_C144“,” _ id“:” 583295bfa0f9f8342035d3b9“} obj已创建并准备存储C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ utils.js:98 process.nextTick(function( ){throw err;}); ^
TypeError:无法在handleCallback处读取C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ routes \\ RegisteredParkingLots.js:76:57的null属性'iparkoUserCount'(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ utils.js:96:12)位于C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ cursor.js:742:16位于handleCallback(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ utils.js:96:12)位于C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ cursor.js:676:5位于handleCallback(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko在setCursorDeadAndNotified(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ node_modules \\ mongodb-core \\ lib \\ cursor.js)中的\\ node_modules \\ mongodb \\ node_modules \\ mongodb-core \\ lib \\ cursor.js:156:5) :496:3)在Cursor.next [as _next](C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ node_modules \\ mongodb-core \\ lib \\ cursor.js:588:12)中\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_mod 光标处的nextObject(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ cursor.js:673:8)处的ules \\ mongodb \\ node_modules \\ mongodb-core \\ lib \\ cursor.js:681:3) .next(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ cursor.js:262:12)位于_each(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ cursor。 js:738:10)位于C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ cursor.js:746:7位于handleCallback(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ utils.js:96:12)位于C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ lib \\ cursor.js:676:5位于handleCallback(C:\\ Users \\ shivendra \\ WebstormProjects \\ iParko \\ node_modules \\ mongodb \\ node_modules \\ mongodb-core \\ lib \\ cursor.js:156:5)
流程以退出代码1完成
您似乎两次打开数据库连接,一个使用mongoose.connect
,另一个使用mongoose.connection.open()
。 这就是为什么您会出错。
尝试仅通过以下一种连接使用此功能。
mongoose.connect(config.database, function(err, db) {
//var dbcon=mongoose.connection.open();
//dbcon.on('error',function(){console.log('connction error:')});
//dbcon.once('open',function(){
if(err) {
console.log(err);
} else {
console.log("WE ARE CONNECTED USING MONGOOSE");
//saving the sample user document
me.save(function (err) {
console.log("in the save func");
if (err) throw err;
else {
console.log('User saved Successfully!!!!!');
res.json({
'whatStatus': 'user saved in the database!!',
'userID': userIDD
});
//mongoose.connection.close();
}
});
}
});
在UserCountCursor.each(...)
循环中,检查err
后,还应该检查doc
。 因此,您在这里:
UserCountCursor.each(function (err, doc) {
if (err)
console.log("did not get the count");
else
// var countString= JSON.stringify(doc);
//...
})
改为这样做:
UserCountCursor.each(function (err, doc) {
if (err){
console.log("did not get the count");
}else if(doc){
// var countString= JSON.stringify(doc);
//...
}
})
然后,您将避免Cannot read property 'iparkoUserCount' of null
错误,并进入save()
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.