无法在猫鼬中按 ID 查找记录
Unable to find record by id in mongoose
我正在尝试按 ID 查找记录,但没有完成
var id = req.param('id');
var item = {
'_id': id
}
videos.find(item, function(error, response) {});
我已经提供了一个有效的 id 但仍然无法获取,请有人提供帮助。
向 find()
提供了回调,但在您上面的代码中,它没有可执行语句。而不是这个:
videos.find(item, function(error, response) {});
...做这样的事情:
videos.find(item, function(error, response) {
if (error) {
console.log(error); // replace with real error handling
return;
}
console.log(response); // replace with real data handling
});
您必须使用回调来处理错误。和 find() returns 数组。如果您需要通过唯一键(在本例中为 _id)查找用户,则必须使用 findOne()
router.get('/GetVideoByID/:id',function(req,res){
var id = req.params.id;
var video = {
'_id' : id
}
videos.findOne(video,function(err,data){
if(err){
console.log(err);
}else{
console.log("Video found");
res.json(data);
}
});
});
我正在尝试按 ID 查找记录,但没有完成
var id = req.param('id');
var item = {
'_id': id
}
videos.find(item, function(error, response) {});
我已经提供了一个有效的 id 但仍然无法获取,请有人提供帮助。
向 find()
提供了回调,但在您上面的代码中,它没有可执行语句。而不是这个:
videos.find(item, function(error, response) {});
...做这样的事情:
videos.find(item, function(error, response) {
if (error) {
console.log(error); // replace with real error handling
return;
}
console.log(response); // replace with real data handling
});
您必须使用回调来处理错误。和 find() returns 数组。如果您需要通过唯一键(在本例中为 _id)查找用户,则必须使用 findOne()
router.get('/GetVideoByID/:id',function(req,res){
var id = req.params.id;
var video = {
'_id' : id
}
videos.findOne(video,function(err,data){
if(err){
console.log(err);
}else{
console.log("Video found");
res.json(data);
}
});
});