Set Object Relationships with API (newbie)

Mission: RELATIONS MASTER
Task: Set Object Relationships with API

Hi,
(I’m new in coding)
I can’t understand what I should do, for create a relationship between two objects. I’ve tried to create an API request in postman.co and past this code :

const APP_ID = ‘6A7C277D-745E-54D3-FFA8-70058389E600’
const API_KEY = ‘1ADAAF05-F9DD-4F00-91B8-143B76150A5D’
Backendless.initApp(APP_ID, API_KEY)

const createParent = async () => {
const person = {
name: ‘Joe’,
age : 25
}

try {
const savedPerson = await Backendless.Data.of(‘Person’).save(person)

return savedPerson

} catch (error) {
console.log('Error saving parent object ’ + error.message)
}
}

const retrieveChild = async () => {
const whereClause = ‘Name = ‘Dallas’’
const queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause(whereClause)

try {
const [childObject] = await Backendless.Data.of(‘City’).find(queryBuilder)

return childObject

} catch (error) {
console.log(‘Error retrieving child object’ + error.message)
}
}

const establishRelationship = async () => {
const [parentObject, childObject] = await Promise.all([
createParent(),
retrieveChild()
])

try {
const count = await Backendless.Data.of(‘Person’).setRelation(parentObject, ‘cityOfResidence:City:1’, [childObject])

console.log('relation has been set')

} catch (error) {
console.log('Error creating a relationship - ’ + error.message)
}
}

establishRelationship()

But it’s not working do I have another option for send my request or is not that to do ?

Regards,
Johan

Hi Johan,

From what I understand about Postman is it can be configured to run an API request using the forms they provide in the app. Did you try pasting the code from the above directly into postman? Can it run JS code? I didn’t think so (perhaps I am missing something new they have added).

In order to run the JS code, you can create an HTML page, import the JS code and then open the page in a browser to run the imported code. However, if you’re new to coding, this might be a rather advanced task to start with (especially with the APIs you’re working with).

Please share with us what your ultimate goal is? Is it to learn to code and use Backendless APIs? Build an app? Perhaps get to the free t-shirt milestone? All are valid ones :wink:

Regards,
Mark

1 Like

Hi Mark,

That is working ! Thank you, I’m trying to learn coding since few mounth with a friend. We would like to build a real applications “without” any real coding. That our goal, because now everything can be build from tools and the community around them. So we will try to use backendless for our backend and AppGyver for the frontend.

Also i’d like to say you thank you for your plateform it’s helping us a lot with tutorials, it’s only for API’s missions that we have problem bcse there is no video explanation :slight_smile:

Regards,
Johan