简体   繁体   中英

Sequelize: Issue with join table and querying for results

Problem exists in the INDEX function, on var friends. 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.

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

Thanks!

var user = require("../../models/index").user; var friendship = 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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