简体   繁体   English

Sequelize:连接表和查询结果问题

[英]Sequelize: Issue with join table and querying for results

Problem exists in the INDEX function, on var friends. 在var朋友上,INDEX函数中存在问题。 I want to set it to an empty array initially so that I can push individual user objects from a friendship join table of users to said array. 我想将其最初设置为一个空数组,以便可以将用户的友谊联接表中的各个用户对象推送到所述数组。 This is because the signed in user could exist on the friendship join table under the userID column or the friendID column, depending on who initiated the friendship. 这是因为已登录的用户可能存在于友谊连接表的userID列或friendID列下,具体取决于谁发起了友谊。

The problem is due to the async nature, when I call console.log(friends) the querying of the database is milliseconds behind, so it is still logging an empty array. 问题是由于异步性质,当我调用console.log(friends)时,数据库查询落后了毫秒,因此它仍在记录一个空数组。 I want the array to be full of user objects who are *NOT the current user, which in essence would be a "myfriends" index page serving up JSON from the backend API. 我希望数组中充满*不是当前用户的用户对象,从本质上讲,这将是一个“ myfriends”索引页面,该页面通过后端API提供JSON。 Keep in mind the fact I'm trying to serve Json, so all the friend user objects HAVE to end up in one single array. 请记住,我正在尝试为Json服务,因此所有朋友用户对象都必须以单个数组结尾。

Thanks! 谢谢!

var user = require("../../models/index").user; var user = require(“ ../../ models / index”)。user; var friendship = require("../../models/index").friendship; var friendly = require(“ ../../ models / index”)。friendship;

var friendshipController = {
  index: function(req, res) {
    var friends = [];
    var request = friendship.findAll({
      where: {
        status: "pending",
        $and: {
          $or: [
            {
              userId: req.session.user.id
            },
            {
              friendId: req.session.user.id
            }
          ]
        }
      }
    }).then(function(friendships) {
      res.json(friendships)
      var friends = []
      friendships.forEach(function(friendship) {
        if (req.session.user.id === friendship.userId) {
          user.findById(friendship.friendId).then(function(user) {
            friends.push(user);
          })
        } else {
          user.findById(friendship.userId).then(function(user) {
            friends.push(user);
          })
        }
      })
      console.log(friends);
    })
  },
  create: function(req, res) {
    friendship.create({
      userId: req.session.user.id,
      friendId: 3,
    }).then(function() {
      res.redirect("/")
    })
  },
  destroy: function(req, res) {
    friendship.findAll().then(function(f) {
      f.forEach(function(fn) {
        fn.destroy()
      })
    })
  }
}

module.exports = friendshipController;

SOLUTION: For each friendship, push each adjacent friend's id of the logged in user to an array...THEN run a query to find all users in that array (a feature of sequelize apparently), then respond with that json data. 解决方案:对于每个友谊,将登录用户的每个相邻朋友的ID推送到一个数组...在运行查询以查找该数组中的所有用户(显然是sequelize的功能)之后,然后使用该json数据进行响应。

index: function(req, res) {
    var friends = [];
    var query = friendship.findAll({
      where: {
        status: "pending",
        $and: {
          $or: [
            {
              userId: req.session.user.id
            },
            {
              friendId: req.session.user.id
            }
          ]
        }
      }
    }).then(function(friendships) {
        var friendIds = [];
        friendships.forEach(function(friendship) {
          if (req.session.user.id === friendship.userId) {
            friendIds.push(friendship.friendId);
          }
          else {
            friendIds.push(friendship.userId);
          }
        });
        user.findAll({
          where: {
            id: friendIds
          }
        }).then(function(users) {
          res.json(users);
        })
    })
  }

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

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