-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Docs] belongsTo() not working in loopback4 - Cannot read property 'target' of undefined #2639
Comments
I tried out your application and it seems the problem is coming from @belongsTo(() => Type)
person_id: number; I tested it with an in-memory connector, but it seems to work if you change Can you try changing it and see if it solves your problem? |
If I changed type_id to type, i see only [
{
"id":1,
"type":1,
"title":"XXXX"
}
] without type's name. What is problem? Expected output [
{
"id":1,
"type": {
"id": 1,
"name": "ABCD"
},
"title":"XXXX"
}
]` |
If you want the actual @belongsTo(() => Type)
type: number; to @belongsTo(() => Type)
type: Type; |
If I changed datatype, id only is returned again instead of object. // Uncomment these imports to begin using these cool features!
// import {inject} from '@loopback/context';
import {Filter, repository} from '@loopback/repository';
import {param, get, getFilterSchemaFor} from '@loopback/rest';
import {TournamentRepository} from "../repositories";
import {Tournament} from "../models";
export class TournamentController {
constructor(
@repository(TournamentRepository)
public tournamentRepository: TournamentRepository,
) { }
@get('/tournaments', /*{...}*/)
async find(
@param.query.object('filter', getFilterSchemaFor(Tournament)) filter?: Filter): Promise<Tournament[]> {
return await this.tournamentRepository.find(filter);
}
} |
Same problem here, I can't figure out how to make loopback lazyload any relations. |
@Ajtak, I haven't got a chance to run your sample. There is a default name for "belongsTo" property, i.e. {modelName}+Id (camel case). So, the property name in your case should be
If you want to keep the
See https://loopback.io/doc/en/lb4/BelongsTo-relation.html#defining-a-belongsto-relation. Hope it helps. cc @b-admike |
@Ajtak, does the above comment help in getting rid of the error? Thanks. |
@dhmlau I was going through a similar situation in my code and found that it actually doesn't work somehow. In my case, I needed the property name in my model class to be camel-cased but the db column name to be snake-cased. As per documentation, 'keyTo' is for specifying column name on target model and 'keyFrom' is for specifying column name on source model. So I used keyFrom. But it threw parse error.
|
@samarpanB, when I was creating the modified CoffeeShop app, I've tried it a bit for My notes was below: The expected foreign key name is in the format of {source-model-name-camel-case}+Id, i.e.
i.e. CoffeeShop Hope it helps. |
@dhmlau I tried that as well with belongs to, it didn’t work either. |
@dhmlau I got it to work finally. Below code works.
For the findOne or find methods to work via juggler, it requires the name key in third parameter of belongsto decorator. For the _createBelongsToAccessorFor function in repository, it requires name key in second parameter. That's how I got it work perfectly. |
I think we need to better document how to do this customization. Adding |
Acceptance Criteria
|
there is some work going on in #1352 that might need this issue to be revalidated. |
@dhmlau examples and test cases for the "keyFrom" parameter are also missing. So I think fixing the docs alone will not be sufficient. |
@deepakrkris, I think if we don't have test cases covered, then we should add it. Supposedly if the docs are clear, we are ok not to have added this in our examples, e.g. the todo-list example. |
AFAICT, we do have tests to verify how to use Here is a model definition that works in our tests: Here are some of the relevant commits & pull requests:
Considering that this issue was reported several months before those two pull requests, I think we need to find out what's the current status. Based on the discussion above and the pull requests I referred to, I think this is the solution: Fix - @belongsTo(() => Type)
+ @belongsTo(() => Type, {name: 'person'})
person_id: number; Fix this.type = this.createBelongsToAccessorFor(
- 'type',
+ 'person',
customerRepositoryGetter,
); Here is what I would like you @deepakrkris to do:
Regarding the example shown by @samarpanB in #2639 (comment). @belongsTo(
() => UserTenant,
{keyFrom: 'created_by', name: 'created_by'},
{
name: 'created_by',
},
)
createdBy?: number; IMO, it's a slightly different scenario. It shows how to use a different property name in LoopBack model & REST API payloads, not the one used by the database. (E.g. change It would be great to have this scenario documented too, but I feel it's a "nice to have" feature in the context of this issue. Plus as you wrote above, we don't have good test coverage for Let's start with documenting the first scenario, where the FK property used the same name as the backing database column. Once that PR is landed, then we can decide whether to work on Regarding example apps, I agree with @dhmlau that we don't need to include these two scenarios in our example apps. We want the example apps to be easy to read and understand, not a kitchen-sink-like behemoth showing everything the framework can do. |
Deferred to Oct, as there is ongoing discussion: https://github.com/strongloop/loopback-next/pull/3808/files#r329042766 |
I have create api which fetch the data from database(PostgreSQL). I have same problem as #2209
Table 1 : type(id, name)
Table 2 : tournament(id, type_id, title)
PostgreSQL structure: https://github.com/Ajtak/lb4-target-error/blob/master/POSTGRESTRUCTURE.sql
Table
type
is belongs to tabletournament
. I want fetch data fromtype
table then respectivetournament
table data will fetch.It will return error : 500 TypeError: Cannot read property 'target' of undefined
Sample repo replicate problem:
git clone https://github.com/Ajtak/lb4-target-error.git
Just I want to get
tournament
with ourtypes
.Thx.
The text was updated successfully, but these errors were encountered: