Node.js 使用Sails.js waterline跨多个模型高效查询

Node.js 使用Sails.js waterline跨多个模型高效查询,node.js,sails.js,waterline,Node.js,Sails.js,Waterline,我试图找到一种有效的方法来检索跨多个模型的结果。在我的示例中,我有三个模型: Video它存储视频数据,包括视频的创建者 User存储用户数据的 Follower哪个存储哪些用户跟随哪些其他用户 我的模型的简化版本如下所示: // Video.js module.exports = { attributes: { title: { type: 'string' }, creator: { model: 'User' } } };

我试图找到一种有效的方法来检索跨多个模型的结果。在我的示例中,我有三个模型:

  • Video
    它存储视频数据,包括视频的创建者
  • User
    存储用户数据的
  • Follower
    哪个存储哪些用户跟随哪些其他用户
我的模型的简化版本如下所示:

// Video.js

module.exports = {

attributes: {
    title: {
      type: 'string'
    },
    creator: {
      model: 'User'
    }
  }
};


// User.js

module.exports = {

  attributes: {
    user_name: {
      type: 'string'
    }
  }
};


 // Follower.js

module.exports = {

  attributes: {
    user: {
      model: 'User'
    },
    follower: {
      model: 'User'
    }
  }
};
Follower.find({where: {follower: queryingUser} , select: ['user']}).exec(function (err, users){

    var userArray = [];

    for (var i=0; i< users.length; i++) userArray.push(users[i].user);

    Video.find({ where: {creator: userArray}, limit: 10, sort: 'createdAt DESC' }).exec(function (err, videos){
        // videos should contain the desired result
    });
});
我意识到我可以将多对多关联存储为
集合
直接存储在
用户
中,但我也会存储其他值,例如用户何时开始跟踪另一个用户以及从哪个目的地跟踪。此外,我存储了大量类似的数据,我希望保持核心模型,如
Video
User
精简

因此,现在我尝试获取10个最新视频(创建日期时间存储在waterline自动添加的
createdAt
属性中),这些视频是由给定用户跟随的用户创建的

从这个给定的用户(我们叫他
queryingUser
)开始,我尝试获取他跟踪的所有用户,并将其用作搜索视频的输入。我想象一个电话是这样的:

// Video.js

module.exports = {

attributes: {
    title: {
      type: 'string'
    },
    creator: {
      model: 'User'
    }
  }
};


// User.js

module.exports = {

  attributes: {
    user_name: {
      type: 'string'
    }
  }
};


 // Follower.js

module.exports = {

  attributes: {
    user: {
      model: 'User'
    },
    follower: {
      model: 'User'
    }
  }
};
Follower.find({where: {follower: queryingUser} , select: ['user']}).exec(function (err, users){

    var userArray = [];

    for (var i=0; i< users.length; i++) userArray.push(users[i].user);

    Video.find({ where: {creator: userArray}, limit: 10, sort: 'createdAt DESC' }).exec(function (err, videos){
        // videos should contain the desired result
    });
});
Follower.find({where:{Follower:queryingUser},select:['user']}).exec(函数(err,users){
var userArray=[];
对于(var i=0;i
我认为,根据测试结果,这样的调用应该会起作用,但是,它似乎并没有检索到任何结果


我做错什么了吗?有没有一种不同的方法可以有效地达到预期的效果?

没关系,它确实有效。我在代码的其他地方犯了一个错误,但在我修复了它之后,我让示例开始工作。也许这对其他人还是有帮助的