-
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
Problem in accessing included relation objects #4472
Comments
user.order.id is giving error |
@Jtmaurya Can you please provide an example repository showing your issue? Please review the issue reporting guidelines: https://loopback.io/doc/en/contrib/Reporting-issues.html#how-to-report-an-issue |
Hi, could you make the issue more descriptive? Following the reporting guidelines would be helpful. For relations,
To query a user with its related
To query an order with its related
|
It is the order repository import {DefaultCrudRepository, repository, BelongsToAccessor} from '@loopback/repository'; export class OrderRepository extends DefaultCrudRepository<
public readonly user: BelongsToAccessor<User, typeof Order.prototype.orderId>; constructor( |
This is the user repository import {DefaultCrudRepository, repository, HasManyRepositoryFactory} from '@loopback/repository'; export class UserRepository extends DefaultCrudRepository<
public readonly orders: HasManyRepositoryFactory<Order, typeof User.prototype.id>; constructor( //////////////// |
@Jtmaurya The repositories seem correct to me. Could you also post those 2 models? thanks. |
import {Entity, model, property, hasMany} from '@loopback/repository'; @model({settings: {strict: false}}) @Property({ @Property({ @Property({ @Property({ @hasmany(() => Order) // Indexer property to allow additional data constructor(data?: Partial) { export interface UserRelations { export type UserWithRelations = User & UserRelations; |
import {Entity, model, property, belongsTo} from '@loopback/repository'; @model({settings: {strict: false}}) @Property({ @belongsTo(() => User) // Indexer property to allow additional data constructor(data?: Partial) { export interface OrderRelations { export type OrderWithRelations = Order & OrderRelations; |
The models seem valid as well. Could you check if your query is correct?
then |
Yes, the above code is working. Can you please help me as i want to get included relation properties. User hasMany orders, So i want to write a query which will give me order.user.user_properties; |
It's the same.
then Is the inclusion not working? or you couldn't query data as you want? I believe the Querying related model section has examples to show the query syntax. |
This code is not working for me. |
Hey @Jtmaurya, can you post an example repo on your account that replicates the issue you are experiencing? |
https://github.com/Jtmaurya/dummy2.git In the main.controller file i want to get included relation properties. |
@Jtmaurya I tired out you app and finally found where the problem is. All the artifacts are valid, just need to make some small changes. The issue is caused by migration. That's why it couldn't create an order instance properly, not to mention for inclusion opertations. Here are the changes you need to make as a workaround: In
|
Could you check what |
`_@get('/orders/abc/{id}', { }`_ Now , if i am writing <res[0].order> or <res[0].user> then it is showing error |
@Jtmaurya I pushed my fix in https://github.com/agnes512/dummy2. Could you try it on your end? I also have 2 examples in the main controller. If everything goes well, that endpoint should create one // the following is for hasMany relation:
const resultUser = await this.userRepository.find({
where: {id: myUser.id},
include: [{relation: 'orders'}],
});
console.log(resultUser);
// the following is for belongsTo relation:
const resultOrder = await this.orderRepository.find({
where: {orderId: order.orderId},
include: [{relation: 'user'}],
});
console.log(resultOrder); |
In the above code we will get user along with the corresponding orders and vice-versa. |
I don't think you can return a instance with user details along with order details together directly. However, your can manipulate the returned object. For example, if you only want the
With this query, the returned related
However, as I mentioned above, if you want to take only the Take the
Don't forget that in HasMany relation, the related model is an array. You can use scope clause to constraint the related model. Similarly, the related model of BelongsTo relation is an object. To include something from the related
|
I am closing this issue as no response. The migration isssue is being tracked in #4744 |
const user = await this.userRepository.findOne({
where: { id: id },
include: [
{ relation: 'order' }
]
});
I am not able to access the related objects .
Relation is User hasMany orders
Order belongsTo user
The text was updated successfully, but these errors were encountered: