簡體   English   中英

我怎樣才能讓這個 JSON 文件中的項目正確顯示?

[英]How can I get the items in this JSON file to display properly?

所以我不確定我在這里做錯了什么。 我從服務中獲得了這三個項目,它采用 JSON 格式。 當我寫出循環以僅顯示藝術家姓名和歌曲時,我不斷收到錯誤消息

這是 server.js 中的 API 命令

app.post('/process_form', function(req, res){
    var search = req.body.search
    axios.get('https://itunes.apple.com/search?term='+search)
    .then((response)=>{
        var songlist = response.data.results;
        console.log(songlist);
        res.render('pages/thanks.ejs', {
            songlist: songlist,
            search: search
        });
    });
})

這是 html 表單的代碼,用戶可以在其中輸入歌曲或藝術家的名稱

        <form action="/process_form" method="post">
            <h1>Which song would you like to lookup</h1>
                <div>
                    <input type="text" id="search" name="search">
                </div>
            <input type="submit">

這是列表中結果為 output 的形式

    <ul>
        <% songlist.forEach(function(songlist) { %>
            <li>
                <strong><%= songlist[artistName] %></strong>
                <strong><%= songlist[TrackName] %></strong>
            </li>
        <% }); %>
    </ul>    

這就是數據的原始來源。

 {
    wrapperType: 'track',
    kind: 'song',
    artistId: 216123617,
    collectionId: 982317323,
    trackId: 982317327,
    artistName: 'Rockabye Baby!',
    collectionName: 'Lullaby Renditions of Radiohead',
    trackName: 'Let Down',
  },

最后我得到的錯誤是。 我只是對為什么說我沒有定義 songList 感到困惑

    5|     <main>
    6|         <ul>
 >> 7|             <% songlist.forEach(function(songlist) { %>
    8|                 <li>
    9|                     <strong><%= songlist[artistName] %></strong>
    10|                     <strong><%= songlist[TrackName] %></strong>

songlist is not defined

對不起,很長的帖子。 我對此感到非常沮喪,我不知道我做錯了什么

編輯:我使用 EJS 作為視圖引擎

app.set('視圖引擎', 'ejs');

我發現解決方案是在實際渲染我的索引文件時,我把

res.render(index)

當文件名應該是

res.render(index.ejs)

暫無
暫無

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

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