Why is the item logging as an object with a parameter, but when trying to access that parameter it's undefined?
My attempts so far:
console.log(item)
=>{ title: "foo", content: "bar" }
, which is fineconsole.log(typeof item)
=> objectconsole.log(item.title)
=> "undefined"
I'll provide some context just in case it's relevant to the issue.
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());
})
}
Following a suggestion, I used debugger
before the problematic line to check what item actually contains via the node repl debugger. This is the result:
After discovering this, I tried console.log(item._doc.title)
and it worked perfectly. So, this seems more like a mongoose-related question now than anything else.
There are similar questions, but they usually involve 'this' accessing objects or attempting to get objects outside of the function scope. In this scenario, I don't believe I'm doing either, but please correct me if I'm mistaken. Thank you.