我想在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})
}
})
}
})
})

答案 0 :(得分:0)
您已将id分配给变量用户,因此您应该:
User.findById(user,function(err,foundUser){ ...
或者你可以req.params.user = foundingdUser.author.id;
如果你出于某种原因想要这样的话可以保持其余部分