I have an object with the below structure:
JavaScript
x
20
20
1
Order: [
2
{
3
id: '001',
4
type: '',
5
status: '',
6
users: [
7
{
8
OrderId:'001',
9
userId: 'String',
10
user: {
11
email: 'string',
12
givenName: 'Name',
13
lastName: 'LastName',
14
phone: 'string',
15
},
16
},
17
],
18
19
},
20
the order is of type Order[] and users of type UserData[]
JavaScript
1
30
30
1
type Order
2
@model
3
@key(fields: ["organizationId", "id"])
4
@auth(
5
rules: []
6
) {
7
id: ID!
8
type: OrderType
9
status: OrderStatus
10
11
userConnections: [UserOrderConnection] @connection(keyName: "byOrder", fields: ["id"])
12
13
organizationId: ID!
14
}
15
16
type UserOrderConnection
17
@model
18
@key(fields: ["organizationId", "id"])
19
@key(name: "Order", fields: ["OrderId"], queryField: "userOrderByOrder")
20
@auth(
21
rules: []
22
) {
23
id: ID!
24
accepted: Boolean
25
OrderId: ID!
26
Order: Order @connection(fields: ["organizationId", "id"])
27
userId: ID!
28
user: UserData
29
}
30
Whenever I try to get the list of users per order :
let users = this.Order.users
it says that: users don’t exist on type Order[], can anyone explain for me why.
Advertisement
Answer
Order is an array of objects, you need to get the first element like Order[0]
then access .users
JavaScript
1
2
1
let users = this.Order[0].users
2
A good refresher might help here; How can I access and process nested objects, arrays or JSON?