是否有什么我遗漏的东西可以允许项目作为带有参数的对象记录,但是当我尝试访问该参数时,它是未定义的?
到目前为止我尝试过的:
console.log(item)
=>{ title: "foo", content: "bar" }
没关系console.log(typeof item)
=> 对象console.log(item.title)
=>“未定义”
我将包括一些上下文,以防万一它与问题相关。
var TextController = function(myCollection) {
this.myCollection = myCollection
}
TextController.prototype.list = function(req, res, next) {
this.myCollection.find({}).exec(function(err, doc) {
var set = new Set([])
doc.forEach(function(item) {
console.log(item) // Here item shows the parameter
console.log(item.title) // "undefined"
set.add(item.title)
})
res.json(set.get());
})
}
根据我debugger
在此行之前删除的建议,以通过节点 repl 调试器检查实际是什么项目。这是我发现的:http : //hastebin.com/qatireweni.sm
从这个我试过console.log(item._doc.title)
,它工作得很好..所以,这现在看起来更像是一个mongoose问题。
有与此类似的问题,但它们似乎与对象的“this”访问有关,或者他们试图将对象置于函数范围之外。在这种情况下,我认为我没有做任何一个,但如果我错了,请告诉我。谢谢