简体   繁体   English

如何获取会员的帖子?

[英]How to get posts by member?

I want to get a post written by a member.我想得到一个成员写的帖子。 in mypage.在我的页面。 But all posts by all members are loaded.但是所有成员的所有帖子都已加载。

user.nick works fine. user.nick 工作正常。 user.id is stored in post. user.id 存储在 post 中。 I tried to compare user.id == post.id but it failed.我试图比较 user.id == post.id 但失败了。

What I did.我做了什么。

page.js 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.ejs我的页面.ejs

<% if(user && user.id) { %>
              <h4>  <%= user.nick %></h2></a>               
                <h4> not <%= twit.posts %></h2></a>               
              <% for ( var i = 0; i < twit.length; i++){ %>
                <h4>  <%= twit[i].posts%></h2></a>
                <h4>  <%= twit[i].id %></h2></a>
                <h4>  <%= twit[i].user.nick %></h2></a>

                  <% } %>
                  <% } %>

index.js 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);

This is where you are doing it wrong:这是你做错的地方:

Post.findAll({    
  include: [{
    model: User,
    attributes : ['id', 'nick']
    // where : { id: req.user}
    // where: { id : Sequelize.col('Post.id') }
    }],
  })

In the above code, you are just including the user object to the post object.在上面的代码中,您只是将user object 包含到post object 中。 It returns all the posts with the associated users .它返回所有相关usersposts

Instead, when you add a hasMany association between User and Post , user instances have access to hasPosts .相反,当您在UserPost之间添加hasMany关联时, user实例可以访问hasPosts

req.user.
    getProducts().
       then((posts) => {
         res.render('mypage', {
            Post: req.food,
            twit : posts,
            user: req.user,
            loginError: req.flash('loginError'),
         });
         console.log(JSON.stringify(posts))
       })
       .catch((error) => {
          console.error(error);
          next(error);
    });

You can query the post which is created by the user like this, if req.user is id of that user :如果 req.user 是该用户的 id,您可以像这样查询用户创建的帖子:

Post.findAll({
    where : { user_id : req.user } // <----- HERE
    include: [{
        model: User,
        attributes: ['id', 'nick']
    }]
})

And if you do not need the user's information with post you can also remove the include如果您不需要用户的信息和帖子,您还可以删除包含

Post.findAll({
    where : { user_id : req.user }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM