Add Objects With Relations Using REST Console not working

Add Objects With Relations Using REST Console not working

Hi, I am trying to make the example from the blog How to Add Objects With Relations Using REST Console. But I can not add location to restaurant through REST Console.

Hi Atanas,

Thank you for posting this question. The article describes the functionality which was available in an older version of Backendless, but has changed in the current version. We’re working on bringing it back and until then the process of creating an object with relations must consist of the following steps:

  1. Save the parent object
  2. Save the child object(s)
  3. Use the add/setRelation API to link the parent and child objects together. The API is documented here. You would need to use something like Postman or cURL (or your own program) to make that API call as the set/addRelation API is not available in REST Console.

Regards,
Mark

Thank you for the quick response,
Regards,
Atanas

Mark,

That doesn’t seem to work or does it?

Thats my handler and I get parsing error with 404:
Could not parse request with message: , status code 404, headers POST /C37FA95F-0D6A-9940-FF9B-AAABADC18D00/438AA7CE-7E50-4CA0-B6E7-6EEE19783B2E/users/82B1A631-F6F6-4A87-9C36-1A4F54B92097/teamId

const endpoints = {
teams: data/teams,
update_user: users/,
update_team: teams/
}

async function setUserTeamId (userId, teamId) {

const token = localStorage.getItem("userToken");
if (!token) {
    throw new Error ("User is not logged in");
}  

return (await fetch(host(endpoints.update_user + userId + "/teamId"), {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "user-token" : token
    },
    body: JSON.stringify([teamId])
})).json();

}

Hello @Darin_Rusev

I don’t see any API Services in your app, that’s why you get 404 Error

Regards, Vlad

@Darin_Rusev, also why not using the SDK?

Guys, I tried…

You mean why not using this?
Because I dont get anything…
const APP_ID = ‘C37FA95F-0D6A-9940-FF9B-AAABADC18D00’;
const JS_API_KEY = ‘A4DDCF0C-62CC-4427-B1DB-399EDF8AA9AE’;
Backendless.initApp(APP_ID, JS_API_KEY);

function host(endpoint) {
return https://api.backendless.com/C37FA95F-0D6A-9940-FF9B-AAABADC18D00/438AA7CE-7E50-4CA0-B6E7-6EEE19783B2E/${endpoint};
}

const endpoints = {
teams: data/teams,
update_user: users/,
update_team: teams/
}

async function setUserTeamId (userId, teamId) {
const token = localStorage.getItem(“userToken”);

if (!token) {
    throw new Error ("User is not logged in");
} 

Backendless.UserService.getCurrentUser()
.then( function( currentUser ) {

    //console.log(currentUser);
   
    currentUser.teamId = teamId;
    return Backendless.UserService.update( currentUser );
})
.catch( function ( error ) {
    return error;
});

// return (await fetch(host(endpoints.update_user + userId + "/teamId"), {
//     method: "POST",
//     headers: {
//         "Content-Type": "application/json",
//         "user-token" : token
//     },
//     body: JSON.stringify([teamId])
// })).json();

}

I dont get even the user found in the first place… and its logged and has a token…

@Darin_Rusev I recommend creating a request with postman or just curl first. Once you got it, then do it in code.

first: in team entity you have to have column (say it is members) and this column has 1 to n relation with users
second: you place post request to team with user/users ids, that you want to add in that column
and finally if you want to have teamId in witch user is - you have to create column in users table, and have to set it manually

the relational columns already exist, created manually in both related tables, indeed “teamId” and “members” but when I use the service, I directly enter into the catch block…
When I was tying to findUserById, I was getting error that its not a function.

I am referring to this:

but have to try referring to node_modules, as this might be the problem

script src="//api.backendless.com/sdk/js/latest/backendless.js"></script

it is not both way relation, the relation is only shown in related table in teams

If you want you can see how I implemented backendless rest api here: https://github.com/astanchev/JS-Applications/blob/master/Lesson%206%20ROUTING/Exercises/Team-Manager-Skeleton/scripts/repository/data.js

Hi @Darin_Rusev

Please read carefully the official documentation for User Object Updating:
https://backendless.com/docs/rest/users_update_user_properties.html

You should use the next url:

https://api.backendless.com/<application-id>/<REST-api-key>/users/<user-id>

Your url is wrong because if contains teamId at the end.
Also the method should be PUT not POST.

I tired the RestAPI Key and the JS API Key…
Moreover even in your link, in the exapmles is written to use the JS API Key;

// do not forget to call Backendless.initApp( appId, jsAPIKey ) in the app initialization code

Tired both PUT and POST today :slight_smile: .For several hours, one can try many things.

Just error returned does not give a hint about the relations issue

Your JS code looks good. Please make sure you log in the user before updating team id. Make sure the current user exists.

Best Regards,
Maksym

Yes, its logged , I get the token.
Even the Postman logs in correctly and gets the token, but gets the same kind of problem. Will check Atanas, who seemed to solve the same problem as I am working on :wink:

Thanks, looking into it. Your call for edit is the same. Seems I have to add user first to the team and then update the user

Correctly