Node.js:返回未定义的文档 - Mongoose

发布于 2024-12-09 21:41:33 字数 715 浏览 0 评论 0原文

Phone.find {number : "12345678"}, (err,phone) ->
            phone.forEach (item, i) ->
                console.log item
                console.log item.subdomain
                console.log item.subdomain_id
                console.log item.city

返回:

{ _id: 4e9b614e01c642c2be000002,
  city: 'San Francisco',
  country: 'US',
  indicative: '234',
  number: '12345678',
  subdomain_id: 4e9b532b01c642bf4a000003 }

undefined
undefined
San Francisco

如果 item.subdomain_id 在文档中,为什么它返回未定义?

编辑:

我将 subdomain_id 添加到架构中,它现在可以工作(item.subdomain_id),但是,我没有获得子域文档,只有 ID。我想要获取 item.subdomain 并能够调用它的方法。

谢谢

Phone.find {number : "12345678"}, (err,phone) ->
            phone.forEach (item, i) ->
                console.log item
                console.log item.subdomain
                console.log item.subdomain_id
                console.log item.city

returns:

{ _id: 4e9b614e01c642c2be000002,
  city: 'San Francisco',
  country: 'US',
  indicative: '234',
  number: '12345678',
  subdomain_id: 4e9b532b01c642bf4a000003 }

undefined
undefined
San Francisco

Why is the item.subdomain_id returning undefined if it's in the document?

Edit:

I added subdomain_id to the Schema and it now works (item.subdomain_id), however, I'm not getting the subdomain document, only the ID. I want to get item.subdomain and be able to call methods on it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-12-16 21:41:33

如果您要存储 ObjectID,而不是嵌入文档,则需要使用 populate() 函数来获取引用的对象:

Phone.find({number : "12345678"}).populate('subdomain_id').run (err, phones) ->
  for phone in phones
    console.log phone

http://mongoosejs.com/docs/populate.html

If you're storing an ObjectID, not embedded documents, you need to use the populate() function to grab the referenced objects:

Phone.find({number : "12345678"}).populate('subdomain_id').run (err, phones) ->
  for phone in phones
    console.log phone

http://mongoosejs.com/docs/populate.html

〗斷ホ乔殘χμё〖 2024-12-16 21:41:33

所以我只是在寻找同一问题的答案。我所做的是使用 Mongoose 的 find (通过对象继承),以便使用 json find 查询。

它看起来是这样的:

User.find({email:req.body.email}).exec(function(err,user){
  console.log(user.email);
}

我忘记并且没有考虑的是返回对象将是一个数组。这是因为您的查找查询可以轻松且经常包含多个对象。因此,您要么需要枚举结果,要么如果您的查询仅期望 findOne(并且我们没有使用 findOne),您可以简单地调用对象 0,如下所示:

User.find({email:req.body.email}).exec(function(err,user){
  console.log(user[0].email);
}

我希望这有帮助,如果你仍然遇到麻烦。

So I was just searching for an answer to the same question. What I was doing was using Mongoose's find (through object inheritance), in order to use a json find query.

What it looked like was this:

User.find({email:req.body.email}).exec(function(err,user){
  console.log(user.email);
}

What I forgot about and hadn't considered is that the return object will be an Array. This is because your find query can easily and often does include multiple objects. Because of this, you will either need to enumerate your results, or if your query is only expected to findOne (and we didn't use findOne), you can simply call object 0 like so:

User.find({email:req.body.email}).exec(function(err,user){
  console.log(user[0].email);
}

I hope this helps, let us know if you are still having trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文