【问题标题】:Sequelize: Issue with join table and querying for resultsSequelize:连接表和查询结果的问题
【发布时间】:2016-09-13 20:11:46
【问题描述】:

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

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

谢谢!

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;

【问题讨论】:

    标签: javascript sql asynchronous sequelize.js


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-01-27
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 2022-08-19
      • 2014-03-28
      • 1970-01-01
      相关资源
      最近更新 更多