我在 Mongoose 中遇到了一个非常奇怪的问题。
此行正确找到Round
:
models.Round.findById("555ec731385b4d604356d8e5", function(err, roundref){
console.log(roundref);
....
这行不
models.Round.findById(result.round, function(err, roundref){
console.log(roundref);
我已经控制台记录了结果
,它显然是一个包含属性 round 的对象:
{round: "555ec731385b4d604356d8e5", selection: 1, time: 20}
为什么如果没有文字,findById
就无法工作?
最佳答案
如果 result
是 JSON 字符串,调用 .round
将返回 undefined
。
首先尝试将 JSON 转换为 JavaScript 对象:
result = JSON.parse(result);
models.Round.findById(result.round, function(err, roundref){
console.log(roundref);
关于node.js - mongoose findById 在我使用字符串文字时有效,但在引用对象的属性时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30695485/