我想在id
中为值req.params.id
分配一个变量,如下所示。
这是我试过的,但是没有用:
router.get("/users/:id", function(req,res){
Campground.findById(req.params.id, function(err,foundingdUser){
var user = foundingdUser.author.id; // i want to use this value for the next findById
if(err) {
req.flash("error", "Something went wrong.");
return res.redirect("/");
}else{
User.findById(req.params.user,function(err,foundUser){ // i want to use the value of user here but it is not working
if(err){
req.flash("error","Something went wrong.");
return res.redirect("/");
}else{
//console.log(foundUser);
res.render("profile",{use: foundUser})
}
})
}
})
})
您已将id分配给变量用户,因此您应该:
User.findById(user,function(err,foundUser){ ...
或者你可以req.params.user = foundingdUser.author.id;
,如果你因为某些原因想要它,那就保持其余部分