[英]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.