javascript - Sequelize : Issue with join table and querying for results

标签 javascript sql asynchronous sequelize.js

问题存在于 INDEX 函数中,在 var friend 上。我想最初将它设置为一个空数组,以便我可以将单个用户对象从用户的友谊连接表推送到所述数组。这是因为登录的用户可能存在于用户 ID 列或 friend ID 列下的友谊连接表中,具体取决于谁发起了友谊。

问题是由于异步性质,当我调用 console.log(friends) 时,数据库的查询延迟了几毫秒,所以它仍然记录一个空数组。我希望数组中充满了 * 不是当前用户的用户对象,这实质上是一个“我的 friend ”索引页面,从后端 API 提供 JSON。请记住,我正在尝试为 Json 提供服务,因此所有 friend 用户对象都必须以一个数组结束。

谢谢!

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

最佳答案

解决方案:对于每个 friend ,将登录用户的每个相邻 friend 的 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);
        })
    })
  }

关于javascript - Sequelize : Issue with join table and querying for results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39478247/

相关文章:

php - 如何检测鼠标光标在div中的位置

sql - 存储数以万亿计的文档相似性

sql - Oracle - 序列依赖于另一列?

python - 当我的 multiprocessing.pool.apply_async 次数超过我的处理器时会发生什么

javascript - Slickgrid-获取选定的单元格值、ID 和字段?

javascript - 按自定义订单排序

Javascript 对象在数组中丢失类型

mysql - 有没有一种简单的方法可以从 Hive 中的托管表创建分区表?

c# - 未调用 OWIN web api CancellationToken

wcf - 如何等待异步 WCF 服务完成?