Is there something that I'm missing that would allow item to log as an object with a parameter, but when I try to access that parameter, it's undefined?
What I've tried so far:
console.log(item)
=>{ title: "foo", content: "bar" }
, that's fineconsole.log(typeof item)
=> objectconsole.log(item.title)
=> "undefined"
I'll include some of the context just in case it's relevant to the problem.
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());
})
}
Based on suggestion I dropped debugger
before this line to check what item actually is via the node repl debugger. This is what I found : http://hastebin.com/qatireweni.sm
From this I tried console.log(item._doc.title)
and it works just fine.. So, this seems more like a mongoose question now than anything.
There are questions similar to this, but they seem to be related to 'this' accessing of objects or they're trying to get the object outside the scope of the function. In this case, I don't think I'm doing either of those, but inform me if I'm wrong. Thanks
You don't have whitespace or funny characters in
' title'
, do you? They can be defined if you've quoted identifiers into the object/map definition. For example:That might cause
console.log(item)
to display similar to what you're expecting, but cause yourundefined
problem when you access thetitle
property without it's preceding space.Are you initializing your object?
If you do not initialize or have not set a title. You will get undefined.
Solution
You can call the
toObject
method in order to access the fields. For example:Why
As you point out that the real fields are stored in the
_doc
field of the document.But why
console.log(item)
=>{ title: "foo", content: "bar" }
?From the source code of mongoose(document.js), we can find that the
toString
method ofDocument
call thetoObject
method. Soconsole.log
will show fields 'correctly'. The source code is shown below:If you only want to get the info without all mongoose benefits, save i.e., you can use .lean() in your query. It will get your info quicker and you'll can use it as an object directly.
https://mongoosejs.com/docs/api.html#query_Query-lean
As says in docs, this is the best to read-only scenarios.
Old question, but since I had a problem with this too, I'll answer it.
This probably happened because you're using
find()
instead offindOne()
. So in the end, you're calling a method for an array of documents instead of a document, resulting in finding an array and not a single document. UsingfindOne()
will let you get access the object normally.Try performing a
for in
loop overitem
and see if you can access values.If it works, it would mean your keys have some
non-printable
characters or something like this.From what you said in the comments, it looks like somehow
item
is an instance of aString
primitive wrapper.E.g.
To verify this theory, try this:
It could also be that
item
is an object that has atoString
method that displays what you see.EDIT: To identify these issues quickly, you can use
console.dir
instead ofconsole.log
, since it display an interactive list of the object properties. You can also but a breakpoint and add a watch.