【问题标题】:node.js: JSON dot notation + access array keynode.js:JSON 点表示法 + 访问数组键
【发布时间】: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].title seems to work
  • 嗨,是的...我更新了我的帖子,其中包含完整的输出。
  • 看起来 sometimes MongoDB 找不到您要查找的文档,因此返回一个空对象。您应该检查回调的err 参数。
  • 谢谢,如果我检查 err 的值,我会得到 'null'
  • 附带说明,如果我尝试这个,那么我确实得到了值:console.dir(books[0].title)

标签: arrays node.js mongodb mongoose syntax


【解决方案1】:

这在 Mongoose(服务器)端不起作用。 find() 的结果是一个 cursor,您需要从本机驱动程序部分通过 toArray 进行迭代或转换。像这样的

(假设架构Books):

Books.collection.find({}).toArray(function(err, results) {

  // results is an array that you can do something with here.

});

当然,如果您为数组及其文档定义了 不是 embedded 的附加架构,那么这会带来更多伤害,您需要使用迭代方法来获取您的结果为数组。

如果您想要books 中的特定 项和其中的子项,那么您最好使用operators 调整您的查找查询,而不是返回所有结果。甚至aggregation pipeline 也可能会有所帮助。

【讨论】:

  • 谢谢你的回答,我仍然需要检查一下,但是如果我需要进行转换,那么这行得通吗? console.log(books[0].title //给我 HTML 的深度
  • @kevinius 你说的是在你的浏览器客户端中检查这个,对吧?如果您将所有结果返回给您的客户,那么您可以通过这种方式进行检查。但你对待它不同服务器端。这就是您在答案中被告知的内容。
  • 我知道我必须在服务器端区别对待。但是输出到 console.log 是“服务器端”吗?所以我可以在服务器和客户端访问books[0].title,但我无法访问数组更深处的值(比如books[0].chapters[0].title。我现在很困惑。 ..对不起...
  • 会不会是因为子文档(章节)没有猫鼬模式?我只设置了这个模式: var booksSchema = new mongoose.Schema({ title: String, author: String });
  • @kevinius 事实是 它实际上不是一个数组,当然也不是我提到的 native 形式,但可能有一个 Mongoose 助手使事物在该级别显示为数组。您可以使用我提出的建议,或者将其移至聊天?就目前而言,您的问题已在解释为什么您尝试做的事情行不通以及该怎么做才能以同样的方式做的意义上得到回答。也是一个指向你可能应该做什么的指针。如果您仍然无法理解这一点,那么可能需要另一个问题。
猜你喜欢
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
相关资源
最近更新 更多