Backendless Support
 
Solved

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:

  1. Backendless.Data.of('Users').addRelation(this.props.currentUser, 'blocked', [this.state.user])
  2. .then(() => console.log('Relation set'))
  3. .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.

Best Answer
photo

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

Leave a Comment

Comments (14)

photo
1

Hi Mudasar

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

photo
1

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.

photo
1

could you please provide your AppId,

and share with us a code snippet for reproduce the problem

thanks Vlad

photo
1

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

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

photo
1

I've checked it with this one:

  1. 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?

photo
1

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

  1. const query = Backendless.DataQueryBuilder.create();
  2. query.setWhereClause(`objectId = '${this.props.currentUser.objectId}'`);
  3. query.setRelated(['friends', 'blocked']);
  4. Backendless.Data.of('Users').find(query).then((user) => {
  5. this.setState({ friends: user[0].friends, loading: false }, () => {
  6. this.props.dispatch(setFriends(user[0].friends));
  7. const blocked = [];
  8. user[0].blocked.forEach((blockedUser) => {
  9. if (blockedUser) {
  10. blocked.push(blockedUser.username);
  11. }
  12. });
  13. console.log('====================================');
  14. console.log(user[0].blocked);
  15. console.log('====================================');
  16. this.props.dispatch(setBlocked(blocked));
  17. });
  18. }).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:

  1. const query = Backendless.DataQueryBuilder.create();
  2. query.setWhereClause(`objectId = '${this.props.currentUser.objectId}'`);
  3. query.setRelated(['friends']);
  4. Backendless.Data.of('Users').find(query).then((user) => {
  5. this.setState({ friends: user[0].friends, loading: false }, () => {
  6. this.props.dispatch(setFriends(user[0].friends));
  7. });
  8. }).catch(error => this.props.alertWithType('error', 'Error Loading Friends', error.message ? error.message : 'No connection'));
  9. query.setRelated(['blocked']);
  10. Backendless.Data.of('Users').find(query).then((user) => {
  11. const blocked = [];
  12. user[0].blocked.forEach((blockedUser) => {
  13. if (blockedUser) {
  14. blocked.push(blockedUser.username);
  15. }
  16. });
  17. this.props.dispatch(setBlocked(blocked));
  18. }).catch(error => this.props.alertWithType('error', 'Error Loading Friends', error.message ? error.message : 'No connection'));

photo
1

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

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

photo
1

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)

  1. 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

photo
1

Yeah seems like the add relation works fine.

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

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

photo
1

thank you, now I can see the problem.

we will find out why it happens and will notify you asap here.

Regards, Vlad

photo
1

Alright thanks man, your support is awesome.

photo
1

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

photo
1

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.

photo
1

Yeah, thank you a lot for it =).

We are glad to improve our JS-SDK and cover all your needs.

Kind regards, Vlad

photo