我正在使用Node.js、Express.js、Handlebars.js和MySQL创建一个可以登录和分享歌曲的网站.我已经建立了一个LikedSong模型,它将喜欢的歌曲与喜欢它的用户关联起来,并设置允许用户喜欢一首歌并try 查看喜欢的歌曲的路由.
问题是,当触发页面查看喜欢的歌曲时,歌曲信息不会显示.
喜欢一首歌可以正确地注册到它在MySQL工作台中的表中,并且在try 将其发送到Handlebar页面之前,我的路径中喜欢的歌曲数组的console.log将返回歌曲的所有信息
点赞歌曲查看路由:
router.get("/liked", withAuth, async (req, res) => {
try {
const likedSongs = await LikedSong.findAll({
where: {
userId: req.session.user_id,
},
include: [Song],
});
// Add liked songs associated with the user to the songs array
const songs = [];
for (let i = 0; i < likedSongs.length; i++) {
songs.push(await likedSongs[i].getSong());
}
console.log("songs =", songs);
res.render("liked", {
songs,
loggedIn: req.session.loggedIn,
});
} catch (err) {
console.log(err);
res.status(500).json(err);
}
});
=
Console.log(‘歌曲=",歌曲)结果:
songs = [
song {
dataValues: {
id: 3,
title: 'Playing God',
artist: 'Polyphia',
album: 'Remember That You Will Die',
embed: '<iframe style="border-radius:12px"\n' +
' src="https://open.spotify.com/embed/track/3nBGFgfRQ8ujSmu5cGlZIU?utm_source=generator"\n' +
' width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay;\n' +
' clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>',
genre_id: 1,
createdAt: 2023-08-26T20:40:08.000Z,
updatedAt: 2023-08-26T20:40:08.000Z
},
_previousDataValues: {
id: 3,
title: 'Playing God',
artist: 'Polyphia',
album: 'Remember That You Will Die',
embed: '<iframe style="border-radius:12px"\n' +
' src="https://open.spotify.com/embed/track/3nBGFgfRQ8ujSmu5cGlZIU?utm_source=generator"\n' +
' width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay;\n' +
' clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>',
genre_id: 1,
createdAt: 2023-08-26T20:40:08.000Z,
updatedAt: 2023-08-26T20:40:08.000Z
},
uniqno: 1,
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
},
song {
dataValues: {
id: 7,
title: 'Solo',
artist: 'Samsa',
album: 'Solo',
embed: '<iframe style="border-radius:12px"\n' +
' src="https://open.spotify.com/embed/track/0oinJU5psqs6JSRT1k9Wfz?utm_source=generator"\n' +
' width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write;\n' +
' encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>',
genre_id: 2,
createdAt: 2023-08-26T20:40:08.000Z,
updatedAt: 2023-08-26T20:40:08.000Z
},
_previousDataValues: {
id: 7,
title: 'Solo',
artist: 'Samsa',
album: 'Solo',
embed: '<iframe style="border-radius:12px"\n' +
' src="https://open.spotify.com/embed/track/0oinJU5psqs6JSRT1k9Wfz?utm_source=generator"\n' +
' width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write;\n' +
' encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>',
genre_id: 2,
createdAt: 2023-08-26T20:40:08.000Z,
updatedAt: 2023-08-26T20:40:08.000Z
},
uniqno: 1,
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
},
song {
dataValues: {
id: 1,
title: 'Music to Clean the House to',
artist: 'Nick Leng',
album: 'LEMONS',
embed: '<iframe style="border-radius:12px" \n' +
' src="https://open.spotify.com/embed/track/18HzHlxWPLoJjEXJdzNf4V?utm_source=generator" \n' +
' width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; \n' +
' encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>',
genre_id: 5,
createdAt: 2023-08-26T20:40:08.000Z,
updatedAt: 2023-08-26T20:40:08.000Z
},
_previousDataValues: {
id: 1,
title: 'Music to Clean the House to',
artist: 'Nick Leng',
album: 'LEMONS',
embed: '<iframe style="border-radius:12px" \n' +
' src="https://open.spotify.com/embed/track/18HzHlxWPLoJjEXJdzNf4V?utm_source=generator" \n' +
' width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; \n' +
' encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>',
genre_id: 5,
createdAt: 2023-08-26T20:40:08.000Z,
updatedAt: 2023-08-26T20:40:08.000Z
},
uniqno: 1,
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
}
]
=
像d.Handlebar一样:
<section class="genre">
{{#if songs.length}}
{{#each songs as |song|}}
<div class="song-details">
<h3>{{song.title}}</h3>
<p>Album: {{song.album}}</p>
<p>By: {{song.artist}}</p>
{{{song.embed}}}
</div>
{{/each}}
{{else}}
<h3>No liked songs found!</h3>
{{/if}}
</section>
=
唯一显示在Liked.Handlebar页面上的是没有试图获取歌曲信息的文本.
仅限如此
"专辑:
发信人:"
或者"找不到喜欢的歌曲!"
如果我还不喜欢一首歌的话.
感谢您能提供的任何帮助!我想解决这个问题已经有一段时间了,但没有成功,如果你需要更多的信息,请告诉我