Problem adding relations in js

I’ve got a one to many relationship in my Users table called ‘blocked’, I add a relationship to it like so:

Backendless.Data.of('Users').addRelation(this.props.currentUser, 'blocked', [this.state.user])
 .then(() => console.log('Relation set'))
 .catch(error => console.log(error));

Which adds the relation, and shows up fine on the backend but when I retrieve it, the objects are undefined instead of user objects for some reason. If I manually add a relation using the console, they’re retrieved fine as user objects. Also, props on 4.0, its way better than 3 and the changes you made for stuff like relations are much appreciated.
Thanks, Mudasar.

Hi Mudasar

is this “this.state.user” saved on the server before you try to add it as relation?

Yeah it is, i can add the relation between the two Users just fine using the console but it has this weird behaviour when using the add/set relation.

could you please provide your AppId,
and share with us a code snippet for reproduce the problem

thanks Vlad

App Id: 2A0EB9A5-1BF6-5C77-FF0C-566B648F7600.

const user = this.state.user;
Backendless.Data.of('Users').addRelation(this.props.currentUser, 'blocked', [user.objectId])
      .then(() => this.props.alertWithType('success', 'Success', `Blocked ${user.username}`))
      .catch(error => this.props.alertWithType('error', `Error Blocking ${user.username}`, error.message ? error.message : 'No connection'));

I’ve checked it with this one:


const user1 = new Backendless.User()
user1.objectId = '13D35FEC-B89D-5C0E-FF1A-640E5CFDC300'

const user2 = new Backendless.User()
user2.objectId = 'EBC60A29-6A3F-6A31-FFEB-FDFE14D2EF00'

Backendless.Data.of('Users').addRelation(user1, 'blocked', [user2])


and it works well, and I can see that in the DevConsole.

please make sure that your "this.props.currentUser’ and "this.state.user’ are already saved on the server before establish a relationship.

btw, when you run/debug your code are you getting “success” or “error”?, if “error” what error.message is?

And also how do you retrieve the relations then?

I get a successful save, no error. And the two users are definitely both saved on the backend.

const query = Backendless.DataQueryBuilder.create();
query.setWhereClause(`objectId = '${this.props.currentUser.objectId}'`);
query.setRelated(['friends', 'blocked']);
Backendless.Data.of('Users').find(query).then((user) => {
this.setState({ friends: user[0].friends, loading: false }, () => {
this.props.dispatch(setFriends(user[0].friends));
const blocked = [];
user[0].blocked.forEach((blockedUser) => {
if (blockedUser) {
blocked.push(blockedUser.username);
}
});
console.log('====================================');
console.log(user[0].blocked);
console.log('====================================');
this.props.dispatch(setBlocked(blocked));
});
}).catch(error => this.props.alertWithType('error', 'Error Loading Friends', error.message ? error.message : 'No connection'));

This returns an array of undefined objects, but if I create the relation using the console it returns an array of user objects with the undefined ones that i set through the api. Oh and a fun note, im adding and retrieving friends the exact same way and dont get an error but with blocking i do.

Just found out if i remove the ‘friends’ from the array of related properties to retrieve, retrieving the blocked users works fine, but i’d like to be able to retrieve both properties in one call.

So this works:

const query = Backendless.DataQueryBuilder.create();
query.setWhereClause(`objectId = '${this.props.currentUser.objectId}'`);
query.setRelated(['friends']);
Backendless.Data.of('Users').find(query).then((user) => {
this.setState({ friends: user[0].friends, loading: false }, () => {
this.props.dispatch(setFriends(user[0].friends));
});
}).catch(error => this.props.alertWithType('error', 'Error Loading Friends', error.message ? error.message : 'No connection'));


query.setRelated(['blocked']);
Backendless.Data.of('Users').find(query).then((user) => {
const blocked = [];
user[0].blocked.forEach((blockedUser) => {
if (blockedUser) {
blocked.push(blockedUser.username);
}
});
this.props.dispatch(setBlocked(blocked));
}).catch(error => this.props.alertWithType('error', 'Error Loading Friends', error.message ? error.message : 'No connection'));

so as I understand the addRelation with API works for you, right?

and the problem is with retrieving, right? not with setRelation?

and also could you please provide a simple code snippet what I can execute in my side?

something like this: (don’t forget specify correct userId)

const query = Backendless.DataQueryBuilder.create();
query.setWhereClause(`objectId = '<userId>'`);
query.setRelated(['friends', 'blocked']);

Backendless.Data.of('Users').find(query)
  .then(users => console.log('users', users))
  .catch(error => console.log('error', error));

thanks

Yeah seems like the add relation works fine.

const query = Backendless.DataQueryBuilder.create();
      query.setWhereClause(`objectId = '9C374D75-996D-6B24-FFE0-22D118BBD400'`);
      query.setRelated(['friends', 'blocked']);
      Backendless.Data.of('Users').find(query).then(users => console.log('users', users))
.catch(error => console.log('error', error));


This should return the friends fine, but return the blocked as undefined objects.

thank you, now I can see the problem.
we will find out why it happens and will notify you asap here.

Regards, Vlad

Alright thanks man, your support is awesome.

the problem has been fixed and released, please upgrade JS-SDK to v4.2.2 and check it.

Let us know if it works for you.

Regards, Vlad

Awesome, thanks! Second time I’ve made you guys update your js sdk haha, also you guys got push notifications to work in react native which is awesome.

Yeah, thank you a lot for it =).
We are glad to improve our JS-SDK and cover all your needs.

Kind regards, Vlad