Updating columns

Hey everyone.

I have in my js class two methods, addOrder(order) and editOrder(order), the addOrder contains a relations using setRelation… and in the editOrder I want to edit the column who has a relation with another table.

I had used this:
"
var whereClause = “objectId=’” + order.id + “’”;
var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause(whereClause);
var orderContent = order.content;
var savedOrder= Backendless.Data.of( “order” ).find( queryBuilder );
var orderData= Backendless.Data.of( “order” )
var contentChanged = orderData.setRelation(savedOrder,“content”,[orderContent]);
"

And then I had invoke a json using editOrder contains “content”:null,
in the build response, the content is null but in the table not changed.

How to change it in the table?

Hi,

Backendless.Data.of( “order” ).find( queryBuilder )
and
rideData.setRelation(savedOrder,“content”,[orderContent])
are asynchronous functions, they return Promise object.
Use async/await or Promise API to get the result.

Regards,
Stanislaw

1 Like

I had tried that, it says in response “Relation Parent must be provided and must be a string or an object with objectId property.”

Provide corrected code please in order to help you to resolve this issue. Your code above is written in sync manner, I explained why it would not work.

This code may work:

var whereClause = “objectId=’” + order.id + “’”;
var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause(whereClause);
var orderContent = order.content;
var savedOrder = await Backendless.Data.of( “order” ).find( queryBuilder );
var orderData = Backendless.Data.of( “order” )
var contentChanged = await orderData.setRelation(savedOrder, “content”, [orderContent]);

Make sure you call it in async function.
Also make sure that orderContent contains object or is ID string.

This means that I tried what you said and this is the response.

So, the error says you to make sure that you have provided parent object (savedOrder in your case). Try to debug your code, see what savedOrder variable contains. I guess it’s empty.

this find method returns an array of objects

const savedOrders = await Backendless.Data.of( “order” ).find( queryBuilder );
const savedOrder = savedOrders[0]
//and make sure that savedOrder is not undefined 

Regards, Vlad

1 Like

Thanks @vladimir-upirov and @stanislaw.grin for these quick responses.

It works with me!