【问题标题】:SailsJs get list of objects population relationshipsSailsJs 获取对象人口关系列表
【发布时间】:2016-07-04 12:22:27
【问题描述】:

我有一个模特帖子:

// Post.js
attributes:{
    name: {type:"string"},
    users: { collection: 'user', via: 'postsReceived' },
    category: { model: 'category' },
    // Plus lots of other fields and relationships to other models for example category, topic, etc...
}

我有一个模型用户:

// Users.js
attributes:{
   name: {type:"string"},
   postsReceived: { collection: 'post', via: 'users', dominant: true }
}

一个帖子可以发送给多个用户,一个用户可能有很多帖子。如果这很好,但是当涉及到填充用户附加到的帖子列表时,这变得非常困难。我正在使用下面的代码来获取帖子列表,但这不会填充帖子关系。例如,下面将只返回帖子类别的 id。让它发挥作用的最佳方法是什么(就性能而言)?

User.findOne({id:id})
.populate('postsReceived')
.exec(function(err, user){
    if(err) return res.serverError(err);
    if (user){
        var result = user.postsReceived;
        return res.json(result);
    }
});

【问题讨论】:

标签: javascript node.js orm sails.js waterline


【解决方案1】:

我找到了一个似乎很好用的解决方案:

This Answer

以上问题的答案建议使用sails-hook-orm-offshore

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-16
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2017-06-22
    • 2015-09-19
    相关资源
    最近更新 更多