1

I have two models Post and User.

I've marked userId property of post with belongsTo decorator

@belongsTo(() => User, {keyTo: 'id', name: 'user'})
userId: number;

but when I use include filter on post.

return await this.postRepository.find({
        "include": [{
            "relation": "user",
        }
        ]});

Loopback throws an error

Unhandled error in GET /posts: 500 Error: Relation "user" is not defined for Post model
at processIncludeItem (/node/posts/node_modules/loopback-datasource-juggler/lib/include.js:309:10)
at /node/posts/node_modules/loopback-datasource-juggler/lib/include.js:187:7
at /node/posts/node_modules/async/dist/async.js:3110:16
at eachOfArrayLike (/node/posts/node_modules/async/dist/async.js:1069:9)
at eachOf (/node/posts/node_modules/async/dist/async.js:1117:5)
at Object.eachLimit (/node/posts/node_modules/async/dist/async.js:3172:5)
at Function.Inclusion.include (/node/posts/node_modules/loopback-datasource-juggler/lib/include.js:185:9)
at /node/posts/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:510:33
at process._tickCallback (internal/process/next_tick.js:61:11)

2 Answers 2

2

As far as i know inclusion of related Models is currently not implemented. see the Inclusion Issue and lack of features compared to previous version

Sign up to request clarification or add additional context in comments.

Comments

1

First of all, create relation by command:

lb4 relation

after that:

return this.postRepository.find(1, {include: [{relation: 'user'}]};)

You can refer here : https://strongloop.com/strongblog/inclusion-of-related-models/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.