Mongoose / MongoDB结果字段在Javascript中显示为未定义 [英] Mongoose/MongoDB result fields appear undefined in Javascript

查看:135
本文介绍了Mongoose / MongoDB结果字段在Javascript中显示为未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些我缺少的东西允许项目作为带参数的对象记录,但是当我尝试访问该参数时,它是未定义的?

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?

到目前为止我尝试过:


  • console.log(item) => {title:foo,内容:bar} ,没关系

  • console。 log(typeof item) => object

  • console.log(item.title) => undefined

  • console.log(item) => { title: "foo", content: "bar" } , that's fine
  • console.log(typeof item) => object
  • console.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 字段中时。

但为什么 console.log(item) => {title:foo,内容:bar}

来自 mongoose(document.js)的源代码,我们可以找到 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中显示为未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆