【发布时间】:2014-02-24 09:59:06
【问题描述】:
我有一个使用 node.js、express、mongoDB 和 mongoose 的站点设置。 Mongoose 给了我一个包含这个的数组:
[ { title: 'HTML in depth',
author: 'kevin vanhove',
_id: 53039f264a6324978c70084b,
chapters:
[ { _id: 53039f264a6324978c70084d,
content: '/jow',
title: 'History of HTML' },
{ _id: 53039f264a6324978c70084c,
content: '/w3c',
title: 'What is the W3C' } ],
__v: 0 } ]
我正在使用此代码在我的终端中获得此结果:
function indexData(books){
console.log(books)
res.render("index.html", {context:books, partials:{footer:"footer", header:"header"}})
}
现在我正在尝试访问 chapters[0].title 中的值,但出现错误:
console.log(books[0].chapters[0].title)
TypeError: Cannot read property '0' of undefined
我在这里感到困惑,我是否使用正确的点符号访问该值?
更新 1:完成终端输出
24 Feb 10:56:47 - [nodemon] restarting due to changes...
24 Feb 10:56:47 - [nodemon] starting `node app.js`
Mongoose: books.find({}) { fields: undefined }
/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/routes/routes.js:45
console.log(books[0].chapters[0].title)
^
TypeError: Cannot read property '0' of undefined
at indexData (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/routes/routes.js:45:32)
at Promise.<anonymous> (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/routes/routes.js:37:31)
at Promise.<anonymous> (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
at Promise.fulfill (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
at /Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/node_modules/mongoose/lib/query.js:1052:26
at model.Document.init (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/node_modules/mongoose/lib/document.js:250:11)
at completeMany (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/node_modules/mongoose/lib/query.js:1050:12)
at Object.cb (/Users/kevinvanhove/Documents/work/projects/basing/business/klanten/javascript/nodeJS/express/basing/node_modules/mongoose/lib/query.js:1017:11)
24 Feb 10:56:49 - [nodemon] app crashed - waiting for file changes before starting...
部分解决方案:
我试图获取章节数据。更新我的猫鼬模式以包含章节数据就可以了。
var booksSchema = new mongoose.Schema({
title: String,
author: String,
chapters: [
{
title: String,
content: String
}
]
});
【问题讨论】:
-
您确定向我们展示了正确的控制台输出吗?复制粘贴 JSON 文档并访问
books[0].chapters[0].titleseems to work。 -
嗨,是的...我更新了我的帖子,其中包含完整的输出。
-
看起来 sometimes MongoDB 找不到您要查找的文档,因此返回一个空对象。您应该检查回调的
err参数。 -
谢谢,如果我检查 err 的值,我会得到 'null'
-
附带说明,如果我尝试这个,那么我确实得到了值:console.dir(books[0].title)
标签: arrays node.js mongodb mongoose syntax