简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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