简体   繁体   English

我怎样才能让这个 JSON 文件中的项目正确显示?

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

So I am not sure what I am doing wrong here.所以我不确定我在这里做错了什么。 I have these three items from a service and it is in JSON format.我从服务中获得了这三个项目,它采用 JSON 格式。 When I write out the loop to just display the artist name and the song I keep getting an error当我写出循环以仅显示艺术家姓名和歌曲时,我不断收到错误消息

This is the API command in server.js这是 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
        });
    });
})

This is the code for the html form where the user can enter the name of a song or artist这是 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">

And this is the form where the results will be output in a list这是列表中结果为 output 的形式

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

This is what the original source of the data looks like.这就是数据的原始来源。

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

Lastly the error I get is that.最后我得到的错误是。 I am just confused as to why it is saying that I did not define songList我只是对为什么说我没有定义 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

Sorry for the long post.对不起,很长的帖子。 I am just super frustrated at this and I do not know what I am doing wrong我对此感到非常沮丧,我不知道我做错了什么

EDIT: I am using EJS as the view engine编辑:我使用 EJS 作为视图引擎

app.set('view engine', 'ejs'); app.set('视图引擎', 'ejs');

I found the solution was that when was actually rendering my index file, I put我发现解决方案是在实际渲染我的索引文件时,我把

res.render(index)

When the file name was supposed to be当文件名应该是

res.render(index.ejs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将JSON文件放入对象并使用AngularJS显示? - How can I get a JSON file into an object and display it using AngularJS? 如何让我的表格正确显示分组数据? - How can I get my table to display grouped data properly? 如何显示文本文件中的项目列表? - How can I display list of items from a text file? 我如何从json获取值并将其显示在javascript中 - How can i get values from json and display them in javascript 如何从 JSON 文件中获取并显示一些图片 - How can I fetch from an JSON file and display some pictures 当我单击它们时,如何让我的按钮正确隐藏/显示? - How can I get my buttons to hide/display properly when I click them? 如何获取代码以显示与JSON文件中的值相关联的字符串数据,而不是返回“ undefined”值? - How can I get my code to display the string data associated with the values in the JSON file instead of returning values of “undefined”? 如何使用ReactJs显示,添加和删除JSON文件中的项目 - How to display, add and delete items from JSON file using ReactJs 如何使用 Javascript 遍历 json 文件中的项目并显示在 HTML - How to loop through items in a json file with Javascript and display in HTML 在用JavaScript替换新图像/标题后,如何获得CSS工具提示以正确显示? - How can I get a css tooltip to display properly after javascript substitutes in a new image/title?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM