Adding list to Backendless.User

i made a query to get a list of orders of user
//user
var newPerson=new Backendless.User()
newPerson.objectId=user[“objectId”];
newPerson.firstName= user[ “firstName” ]
newPerson.lastName= user[ “lastName” ]
var whereClause = “ownerId=’”+user[“objectId”]+"’";
var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause(whereClause);
//storing the result of query in newPerson.orders
newPerson.orders=Backendless.Data.of( “order” ).find( queryBuilder )
.then( function( orders) {
return orders
})
.catch( function( fault ) {

});
return newPerson;
output:
{
“___jsonclass”: “Users”,
“firstName”: “aadel”,
“lastName”: “kanso”,
“orders”: {},
“___class”: “Users”,
“objectId”: “F6AD65BB-D2FD-099F-FF20-D68A4AA76600”,
}
the query it self return a list of orders but whenever i want to store this list in newPerson.orders the result become {} as shown in output

Hello @adel_kasno

In order to establish relationship between objects you need to use set/addRelation API
https://backendless.com/docs/js/data_set_relation_with_objects.html

Regards, Vlad

there isn’t a relationship , i’m working with ownerId
please if you can re-read my problem
its about to store the return object coming from Backendless.Data.of( “order” ).find( queryBuilder )

Hi Adel,

From what I understood, when you store newPerson you also want to store the orders of that person. Since the order objects are stored in a separate from the person table (the order table), it becomes a relation in the Backendless lingo. So @vladimir-upirov’s answer is actually correct.

Regards,
Mark

I’m trying to get objects not storing them
i set up the relationship…
and i turned on “AutoLoad”
so when retrieving person the orders also being retrieved
but right now i want to retrieve items and ther is orderId in items table
so how can i retrive person orders items
the structure is like this
person(order id 1:1)
order()
items(order_id 1:N)

Regards

No problem, I just need a little help so I can understand your schema better. If I understood correctly, you have the following tables:

  • person
  • order
  • items

The person table has a one-to-one relationship to order.
The items table has a one-to-many relation to order.

Am I correct so far?

And what you’re asking is how to retrieve items for some specific person’s order. Is that also correct?

Regards,
Mark

Correct this is the relation and that’s what i’m trying to do

Once you know order’s objectId, you can structure the following query (I assume the name of the relation column between items and order is order_id:

var whereClause = "order_id.objectId='" +  VALUE_OF_ORDER_OBJECTID  + "'";
var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause(whereClause);
newPerson.orders=Backendless.Data.of( "items" ).find( queryBuilder )
.then( function( items ) {
  return items
})
.catch( function( fault ) {

});

This will retrieve all the items for a particular order.

1 Like

method find returns an instance of Promise,
so to assign the items list to newPerson as orders you have to use async/await or do it inside the then function

newPerson.orders = await Backendless.Data.of( "items" ).find( queryBuilder )

or

Backendless.Data.of( "items" ).find( queryBuilder )
.then( function( items ) {
  newPerson.orders = items
})
.catch( function( fault ) {

});
1 Like

Problem solved ,
yes find return a Promise so i must use async/await
Thank you @mark-piller @vladimir-upirov
i ended up with this
async function myFx(user){
var newPerson=new Backendless.User()
newPerson.objectId=user[“objectId”];
newPerson.firstName= user[ “firstName” ]
newPerson.lastName= user[ “lastName” ]
var whereClause = “order_id.objectId=’” + user.order.objectId + “’”;
var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause(whereClause);
newPerson.orders=await Backendless.Data.of( “items” ).find( queryBuilder )
.then( function( items ) {
return items
})
.catch( function( fault ) {

});
}
also the @vladimir-upirov the second code doesn’t work , but as i said async/await work as expected
Regards

glad to hear that!

the second option doesn’t work because this is async code

Backendless.Data.of( "items" ).find( queryBuilder )
.then( function( items ) {
  newPerson.orders = items // it will be invoked after this: newPerson.orders === undefined 
})

newPerson.orders === undefined // this is expected behaviour, and it will be invoked before newPerson.orders = items  

btw, you do not need to specify then if you use await, just use

newPerson.orders=await Backendless.Data.of( “items” ).find( queryBuilder )

you can read more about async/await here https://javascript.info/async-await

Regards, Vlad

Thanks for informing me, i’m new at async await but i will read more about it