Add New Object with a Exist Object inside

Hi all,
i have this objects Rooms and Reservations whit a relationship(Reservations have a Room field inside)
When i want to save a new Reservation, the room field is a exist object, then i try with this call:

{
"dateCheckIn": "02/02/2018",
"dateCheckOut": "02/02/2018",
"nameReservation": "tizio",
"room": {
"objectId":"A686A91D-F6F4-1C8B-FF19-5EDA53257000",
"___class":"Rooms"
}
}

The object Reservation save successfully, but the filed room remain empty.
The service’s code is the seguent:

/**
 * @param {Reservations} reservation
 * @returns {Promise.<void>}
 */
 addReservation(reservation) {
 return Backendless.Data.of(Reservations).save(reservation);
 }

What i wrong?
thank
GF

Reservations is a class, do you have also a field for the objectId?

You have to use relations api to tie objects https://backendless.com/docs/js/doc.html#data_relations_api_set_add_js

i have add the relation between Rooms and Reservations (see schema_reservation.png)

My problem is i want to use the service and when i call “addReservation(reservation)” method how should pass the room objectId?
Thank
GF

yes, Reservation have the field room (see ‘schema_reservation.png’ in the my response of Sergey Kukurudzyak)

let reservation = ...// save reservation
let rooms = ...// save rooms

Backendless.Data.of( "Reservation" ).setRelation( 
     reservation,
     "room",
     rooms )
 .then( function( count ) {
      //count is a number of created relations
  })
 .catch( function( error ) {
  });

sorry, maybe i explained myself wrong.
From my app i call the api of the service i created, then when i want to add a reservation, i call (for example):

https://api.backendless.com/XXXXXXXXX/XXXXXXXXXX/services/Service/Reservation 

with a body (for example):

{
 "dateCheckIn": "02/02/2018",
 "dateCheckOut": "02/02/2018",
 "nameReservation": "alador",
 "room": {
 "objectId":"A686A91D-F6F4-1C8B-FF19-5EDA53257000", 
 "___class":"Rooms"
 }
}

Then, i don’t need to save a room, because the room already exists (so i already have the objectId).

Finally, my questions are:

  1. How should the body of the call be?
  2. How do i write the “reservation” method of the service?
  3. The relation is present in the schema, do you still have to set up the relation every time?

Thank
GF

  1. as this is your service you are responsible to provide correct body, so I cannot tell what the body should be

  2. what does this method should do? if create relation then

Backendless.Data.of( "Reservation" ).setRelation(
reservation,
"room",
rooms )
.then( function( count ) {
//count is a number of created relations
})
.catch( function( error ) {
});

where reservation is Reservation object with contains not empty “objectId” property,
rooms array wich cotains Rooms objects which contains not empty “objectIds”

  1. you have to call setRelation or addRelation method every time you want to create relation.

Perfect, thank for the response.

I have tested whit this method in the ‘Api Service’ but i receive this error:
400 - Invalid value for the “parent” argument. The argument is required and must contain only string or object values.

What did i wrong?

this is the service’s method:

/**
  * 
  * 
  * @param {Reservations} reservation
  * @param {Rooms} room
  * @returns {Promise.<void>}
  */
  addReservation(reservation, room) {
    
    let reservationSaved = Backendless.Persistence.of(Reservations).save(reservation);
    
    return Backendless.Data.of( Reservations ).setRelation(reservationSaved,"room", room );
    
  }
and this the call's body :
{
    "reservation": {
        "dateCheckIn": "02/02/2018",
    	"dateCheckOut": "02/02/2018",
    	"nameReservation": "alador",
    	"fromFacebook": false,
        "fromGroupon": false,
        "note": "string",
        "totalPrice": "string"
    },
    "room": {
        "objectId":"A686A91D-F6F4-1C8B-FF19-5EDA53257000", 
"___class":"Rooms"
    }
}

thank
GF

you can not use a synchronous methods in business logic. your code should be:

  
addReservation(reservation, room) {

return Backendless.Persistence.of(Reservations).save(reservation)
.then( function( savedObject ) {
return Backendless.Data.of( Reservations ).setRelation(reservationSaved,"room", room )
.then( function( savedObject ) {
  })
 .catch( function( error ) {
  });
  })
 .catch( function( error ) {
  });




}

also room should be an array, for example:

"room": [{
"objectId":"A686A91D-F6F4-1C8B-FF19-5EDA53257000",
"___class":"Rooms"
}]

thanks for the response Sergey,

i used your code and when i test the service the response is ok (200) and save the reservation object in db, but the field room is empty.
What did i wrong?

please attach js file with a service to check it

this is service file:

/*Created on 01/05/2018 09:57:34.*/
'use strict';


const Reservations = require('../models/reservations');
      
class ReservationsService {
  
  /**
  * 
  * 
  * @param {Reservations} reservation
  * @param {Rooms} room
  * @returns {Promise.<void>}
  */
  addReservation(reservation, room) {
    return Backendless.Persistence.of(Reservations).save(reservation)
    .then( function( savedObject ) {
        return Backendless.Data.of( Reservations ).setRelation(savedObject,"room", room );
    })
    .catch( function( error ) {
      return error;
    });
    
  }
  
}
 
Backendless.ServerCode.addService(AladorReservationsService);

and this is the Reservation model:

/*Created on 01/05/2018 10:24:50.*/
'use strict';
 
/**
 * @property {DateTime} dateCheckIn
 * @property {DateTime} dateCheckOut
 * @property {String} nameReservation
 * @property {Rooms} room
 */
class Reservations extends Backendless.ServerCode.PersistenceItem {
  
}
 
module.exports = Backendless.ServerCode.addType(Reservations);

what is your application id?