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