1

The value of

found.totalfunds = 99

But when im assigning it to variable

funds

it returns undefined on console.log(funds)

 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); This returns a number I made as default.

1 Answers1

1

Society.findById has a callback because it's asynchronous.

You have to modify your code to continue only when the callback gets executed:

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});
     });
});
Sombriks
  • 3,370
  • 4
  • 34
  • 54