Node.js How do I get a logged-in user post?

Asked 2 years ago, Updated 2 years ago, 148 views

When a member enters my page, he or she wants to print out only what he or she wrote.

I'm currently printing all the writings that other members wrote. Only the user's nickname is properly brought.

I don't know if I'm searching well or if there's anything I can help with, please give me a link.

page.js

router.get('/', (req, res, next) => {
Post.findAll({    
  include: [{
    model: User,
    attributes : ['id', 'nick']
    // // where : { id: req.user}
    // // where: { id : Sequelize.col('Post.id') }
    }],
  })
    .then((Post) => {
      res.render('mypage', {
        Post: req.food,
        twit : Post,
        user: req.user,
        loginError: req.flash('loginError'),
      });
      console.log(JSON.stringify(Post))
    })
    .catch((error) => {
      console.error(error);
      next(error);
    });

mypage.js

<% if(user && user.id) { %>
              <h4> Good for you <%=user.nick%></h2></a>               
                <h4> No <%=twit.posts%></h2></a>               
              <% for ( var i = 0; i < twit.length; i++){ %>
                <h4> Value <%=twit[i].posts%></h2></a>
                <h4> ID value <%=twit[i].id%></h2></a>
                <h4> Nickname value <%=twit[i].user.nick %></h2></a>

                  <% } %>
                  <% } %>

index.js

db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.User = require('./user')(sequelize, Sequelize);
db.Post= require('./Post')(sequelize, Sequelize);

db.User.hasMany(db.Post);
db.Post.belongsTo(db.User);

node.js sequelize.js ejs mysql

2022-09-22 15:14

1 Answers

? Shouldn't we put a condition?

Post.findAll({
    where: {user: req.user}, // <-- something like this
    include: [{
        model: User,
        attributes : ['id', 'nick']
    }]
}) // omitted below


2022-09-22 15:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.