问题描述
是否有什么我遗漏的东西可以允许项目作为带有参数的对象记录,但是当我尝试访问该参数时,它是未定义的?
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" }
,没问题console.log(typeof item)
=> objectconsole.log(item.title)
=>未定义"
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());
})
}
根据建议,我在此行之前删除了 debugger
以通过节点 repl 调试器检查实际是什么项目.这是我发现的:http://hastebin.com/qatireweni.sm
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
从这里我尝试了 console.log(item._doc.title)
并且它工作得很好..所以,这现在看起来更像是一个猫鼬问题.
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.
有一些与此类似的问题,但它们似乎与对象的this"访问有关,或者他们试图将对象置于函数范围之外.在这种情况下,我认为我没有做任何一个,但如果我错了,请告诉我.谢谢
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
推荐答案
解决方案
您可以调用 toObject
方法来访问字段.例如:
Solution
You can call the toObject
method in order to access the fields. For example:
var itemObject = item.toObject();
console.log(itemObject.title); // "foo"
为什么
正如您所指出的,实际字段存储在文档的_doc
字段中.
Why
As you point out that the real fields are stored in the _doc
field of the document.
但是为什么 console.log(item)
=> { title: "foo", content: "bar" }
?
But why console.log(item)
=> { title: "foo", content: "bar" }
?
来自mongoose的源代码(文档.js),我们可以发现Document
的toString
方法调用了toObject
方法.所以 console.log
将正确"显示字段.源代码如下所示:
From the source code of mongoose(document.js), we can find that the toString
method of Document
call the toObject
method. So console.log
will show fields 'correctly'. The source code is shown below:
var inspect = require('util').inspect;
...
/**
* Helper for console.log
*
* @api public
*/
Document.prototype.inspect = function(options) {
var isPOJO = options &&
utils.getFunctionName(options.constructor) === 'Object';
var opts;
if (isPOJO) {
opts = options;
} else if (this.schema.options.toObject) {
opts = clone(this.schema.options.toObject);
} else {
opts = {};
}
opts.minimize = false;
opts.retainKeyOrder = true;
return this.toObject(opts);
};
/**
* Helper for console.log
*
* @api public
* @method toString
*/
Document.prototype.toString = function() {
return inspect(this.inspect());
};
这篇关于Mongoose/MongoDB 结果字段在 Javascript 中显示为未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!