<分区>
我正在学习nodejs和express,在学习期间我不明白req
的各种用法比如:
var id = req.params.id;
var wine = req.body;
对于我的 req
对象,我认为正在访问 body, params.id
,但我不明白这是如何工作的以及我应该期待什么结果。谁能以更简单的方式逐步解释这段代码,以便我理解它?
下面是一些使用上述内容的示例代码:
exports.addDoctor = function(req,res){
var doctor = req.body;
console.log(doctor);
db.collection('doctors',function(err,collection){
collection.insert(doctor,{safe:true},function(err,result){
if (err) {
res.send({'error':'An error is occured'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
}
在上面的示例中,console.log(doctor)
仅返回 {}
。为什么会这样,是不是因为我错过了代码中特别涉及“req”的其他内容?