As a novice in the world of programming, I am currently immersed in a website project where users can write and post articles. One crucial aspect I am focusing on is displaying the history of articles written by each user on their account page. Despite utilizing sequelize and handlebars for this purpose, I am encountering an issue where the user's username appears correctly but not the titles of their articles in the History Section.
Seeking solutions to address this dilemma, I have set up my controller to reference both the User and Story models to filter out stories authored by the current user. Subsequently, I employ res.render to pass these variables to my hbs page:
getAccount: async (req, res) => {
const user = await User.findOne({where: {username: req.session.username}})
const stories = await Story.findAll({
where: {
userId: user.id
}
})
console.log(stories, stories.title);
stories.forEach(story => {console.log(story.title);})
res.render('user_account', { user, stories })
}
The structure of my HTML page utilized for rendering this information is as follows:
<div class="user-title">
<h1>Hello {{username}} </h1>
</div>
<div class="storyList-pannel">
<h2>History</h2>
<div class="articles-pannel">
{{#each stories}}
<div class="article-title">
<h3> {{title}} </h3>
</div>
{{/each}}
However, upon inspecting the console, I notice that the title is returning 'undefined'. Oddly enough, I am able to retrieve its value when iterating through it with a forEach loop, leaving me puzzled regarding the cause of this discrepancy.