简体   繁体   English

SailsJs获取对象的人口关系列表

[英]SailsJs get list of objects population relationships

I have a model Post: 我有一个模特帖子:

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

And I have a model Users: 我有一个模型用户:

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

A post can be sent to many users and a user may have many posts. 帖子可以发送给许多用户,用户可能有很多帖子。 This if fine however when it comes to populating a list of posts that a user is attached to this becomes quite difficult. 这很好,但是当填写用户附加到其上的帖子列表变得非常困难时。 I am using the below code to get the list of posts, however this does not populate the posts relationships. 我使用下面的代码来获取帖子列表,但是这不会填充帖子关系。 For example the below will only return a id for the category of the post. 例如,下面只会返回帖子类别的ID。 What is the best way (in terms of performance) to get this to work? 要使其发挥作用,最佳方式(在性能方面)是什么?

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

I found a solution for this that seems to work quite nicely from: 我找到了一个解决方案似乎很好地工作:

This Answer 这个答案

The answer in the question above suggets using sails-hook-orm-offshore 上面问题的答案建议使用sails -hook-orm-offshore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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