Node.js: Document returning undefined - Mongoose(Node.js:文档返回未定义 - Mongoose)
问题描述
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
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.
这篇关于Node.js:文档返回未定义 - Mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Node.js:文档返回未定义 - Mongoose


- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01