我是Node.JS的新手,无法找到解决方案。 我正在构建一个宁静的服务来从mongo中检索数据。Node.js:无法返回Mongoose查找结果
app.js
var database = require('./database.js');
...
app.get('/metadata', function(req, res) {
console.log("GET: metadata");
res.send(database.getMetadata(null));
});
app.get('/metadata/:id', function(req, res) {
console.log("GET: metadata");
res.send(database.getMetadata(req.params.id));
});
...
database.js
exports.getMetadata = function (id) {
console.log('getting metada...')
if (id == null) {
return FolderInfo.find({}, null, null, function (err, metadatas) {
if (!err) {
console.log(metadatas);
return metadatas;
} else {
return console.log(err);
}
});
}
else {
return FolderInfo.findById(id, function (err, metadatas) {
if (!err) {
return metadatas;
} else {
return console.log(err);
}
});
}
}
我已经尝试了许多不同的方法。但总是得到这样的结果:
{
options: {
populate: { }
},
_conditions: { },
_updateArg: { },
op: "find"
}
但我的console.log(metadatas);行将结果打印到控制台。 我正在使用所有最新的软件包。 有人能帮助我吗?
你的意思是即使你提供了一个ID参数在GET请求中,泛型find(而不是findByID)被调用? – shelman
如果不是,你希望的行为是什么? – shelman
find方法似乎可行,因为回调中的console.log会打印结果,但这不是我回复的结果。 –