0
我需要查找用戶的事件,但我無法檢索用戶,因爲我無法傳遞用戶標識來路由事件文件。將參數傳遞給路由文件
app.js:
var Router = express.Router();
Router.use("https://stackoverflow.com/users/:uid/events", events);
events.js:
Router.post("/",function (req,res) {
User.findOne({
_id:req.params.uid
},function (err, foundUser) {
if(err){
res.json({success:false, message:"The user couldn't be found"});
}else{
console.log(req.params.uid);
console.log(foundUser);
var event = new Event();
event.title = req.body.title;
event.description = req.body.description;
event.save(function (err) {
if(err){
console.log(err);
}
});
foundUser.events.push(event);
}
})
});
結果:
uid = undefined
foundUser = null
如何傳遞uid參數的events.js?