簡體   English   中英

為什么我的 pug 文件不顯示我檢索到的數據?

[英]Why isn't my pug file displaying my retrieved data?

在我的 index.js 文件中,當我使用 console.log 查看數據時,我可以確認它已被檢索到。 當我嘗試將該數據放入我的視圖時,我收到一條錯誤消息: "Cannot read property 'feedUrl' of undefined.

這是 console.log 返回的內容,這些數據來自后端,它是我想要在視圖中顯示的數據。 特別是 feedUrl 屬性:

[
  {
    _id: 5ec4580bb82c6c10a07936b6,
    feedUrl: 'http://rss.cnn.com/rss/cnn_topstories.rss',
    __v: 0
  },
  {
    _id: 5ec463ef7dd1c2d7b32a74bc,
    feedUrl: 'http://rss.cnn.com/rss/cnn_world.rss'
  }
];

這是 index.js 中處理此路由的代碼:

// bring in the models I'll be using
let rssLink = require('./models/rssLink')
let rssItem = require('./models/rssItem')

// set up template engine and specify where the views will come from 
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')

// home route
app.get('/', (req, res) => {
    // query data base for links already stored, if any
    rssLink.find({}, function(err, feedlinks){
        console.log(feedlinks)
        if (err) {
            console.log(err)
        } else {
            res.render('index', {
                title: 'Welcome! Please register below',
                feedlinks: feedlinks
            })
        }
    })
})

這是我的哈巴狗文件(視圖):

block content
    h1 #{title}
    ul each val, i in feedlinks
        li= val.feedUrl

請讓我知道如何克服這個問題。 提前致謝。

那是縮進。 我覺得很愚蠢,但我把它留在這里以防萬一其他人遇到這個問題。 這是正確的縮進:

block content
    h1 #{title}
    ul 
        each val in feedlinks
            li= val.feedUrl

暫無
暫無

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

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