You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Question: best way to aggregate relations of a model instance #198
I am trying to create a remoteMethod that will return the requested object and all its related sub objects (like getting a User and all his posts at the same time).
So Im doing the following:
User.full=function(id,callback){User.findById(id,function(err,user){user.posts(function(err,posts){//user.posts = posts; << this is completely ignoredcallback(err,user,posts);})});}loopback.remoteMethod(User.full,{description: 'User+Posts',accepts: [{arg: 'id',type: 'String',required: true,description: 'The id of the user'}],returns: [{arg: 'user',type: 'object'},{arg: 'posts',type: 'array'}]});
I have a few issues with this:
It does not reproduce the structure of the object (I have a "user" property and a "posts" property at the same level)
I tried affecting the "posts" to user.posts before launching the callback, but it looks like since there is a "posts" function declared in the prototype of the model it is ignored completely.
So how should I go about it if I want to fetch a model instance and all its related instances and return them nicely?
I am trying to create a remoteMethod that will return the requested object and all its related sub objects (like getting a User and all his posts at the same time).
loopback.remoteMethod(
User.full,
{
description: 'User+Posts',
accepts: [
{arg: 'id', type: 'String', required: true, description: 'The id of the user'}
],
returns: [
{arg: 'user', type: 'object'},
{arg: 'posts', type: 'array' }
]
}
);
I have a few issues with this:
It does not reproduce the structure of the object (I have a "user" property and a "posts" property at the same level)
I tried affecting the "posts" to user.posts before launching the callback, but it looks like since there is a "posts" function declared in the prototype of the model it is ignored completely.
So how should I go about it if I want to fetch a model instance and all its related instances and return them nicely?
—
Reply to this email directly or view it on GitHub.
Activity
raymondfeng commentedon Feb 23, 2014
I put together an example, please check it out at:
https://github.com/strongloop-community/loopback-example-datagraph
Thanks,
Raymond
On Feb 22, 2014, at 4:17 PM, Cadrach notifications@github.com wrote:
Cadrach commentedon Feb 23, 2014
Thanks, the example allowed me to do exactly what I wanted.