簡體   English   中英

從結果 node.js 記錄 MongoDB 收集數據

[英]log MongoDB collection data from results node.js

我有這個,它工作完美:

socialPosts.find({}).toArray(function(err, result) {
            if (err) throw err;
            console.log(result);

            db.close();
        });

它記錄集合中的所有文檔,如下所示:

    {
    _id: 60dcaf07539e5d7d2ebc5180,
    title: "my Second Post",
    description: 'my second post',
    socialLink: 'sociallink.com/sociallink'
  {
    _id: 60dcc2a250351a7efc5f0e46,
    title: 'hello',
    description: 'hello',
    socialLink: 'hello'
  }

但是,我想將這些值傳遞給前端,所以我想從前端訪問titledescription等,但理想情況下我希望能夠首先記錄它們。 所以我嘗試了console.log(result.title) ,但沒有用。 不知道如何做到這一點。 謝謝

談到tnaluat評論,我能夠使用提到的內容並循環通過。

 socialPosts.find({}).toArray(function(err, result) {
            if (err) throw err;
            for (var i = 0; i < result.length; i++) {
                console.log(result[i].title)
            }


           
           res.render('media.ejs', {data: result})

            db.close();
        });

然后在前端,我通過 ejs 中的循環訪問data

單線解決方案:

result.forEach(item => console.log(item.title));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM