MongoDB字段返回一个数字,但未定义javascript变量



的值

found.totalfounds=99

但当我将其分配给变量时

资助

它在控制台上返回undefined。log(资金(

router.get("/:id/managefunds/",middleware.isLoggedIn,function(req,res){
console.log("Manage Funds");
var societyId = req.params.id;
// res.send(societyId);
var funds ;
Society.findById({_id:societyId},function(err,found){
console.log("fromMongo"+found.totalFunds);
funds = found.TotalFunds;
});
console.log("FUNDSSSS: "  + funds);
res.render("campgrounds/managefunds",{parm:societyId,funds});
});

console.log("fromMongo"+found.totalFunds(;这将返回一个我设置为默认值的数字。

Society.findById有一个回调,因为它是异步

你必须修改你的代码,只有当回调被执行时才能继续:

router.get("/:id/managefunds/",middleware.isLoggedIn,function(req,res){
console.log("Manage Funds");
var societyId = req.params.id;
// res.send(societyId);
var funds ;
Society.findById({_id:societyId},function(err,found){
console.log("fromMongo"+found.totalFunds);
funds = found.TotalFunds;     console.log("FUNDSSSS: "  + funds);
res.render("campgrounds/managefunds",{parm:societyId,funds});
});
});

相关内容

最新更新