Display model properties with sequelize and handlebars

  Kiến thức lập trình

I’m a beginner in programming and I’m currently working on a website project : a basic one where the user can write and post articles.
On the user account page, I want to display the history of articles written by the user. For this, I’m using sequelize and handlebars, but the problem is that my page display the username, but not the article’s title in the History Section.
So I was wondering if you got some solutions for my problem.

In my controller, I take reference from my User and Story models to get only the stories written by the current user. Then I make a res.render of my variables to use them in my hbs page :
My controller :

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 })
    }

My page :

<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}}

In my console, I see that the title is undefined but I still can get it value when I make a forEach loop on my variable, which I don’t really understand why.

New contributor

CooperPeaks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT