1

I have a database setup with a couple of different objects. For example lets say I have an object called 'kids' and a related object called 'toys'. Toys has a 'kid' id field which references the kid. What I would like is that when I create a kid I can also send a list of toys and have it automatically add those toys.

I created an action in the After Create section, but whenever I check the 'userInput' variable my extra data isn't there ( I'm using the test function in the app ). Consider the following:

return $http ({
  method: 'POST',
  url: Backand.getApiUrl() + '/1/objects/kids/',
  params: {
    name: 'Add Kid',
    parameters: {}
  },
  data: {
    name: 'Jimmy',
    toys: ['truck', 'car', 'crane', 'motorbike'],
  }
});

and then in the callback

function backandCallback(userInput, dbRow, parameters, userProfile){
    console.log(userInput);
    for ( var i = 0; i < userInput.toys.length; i++ ){
        var toy = userInput.toys[i];

        //use $http POST method to create a toy which relates to the user
    }
}

Whenever I examine the userInput variable all I get is a json object with 'name' in it. However I would expect 'toys' to also be there.

I did wonder if maybe I was taking the wrong approach, should I be making a call to the api to create a kid and then take the data from that and make multiple api calls to create each toy?

thanks for your help

Matt Sanders
  • 370
  • 2
  • 12

1 Answers1

0

You need to add deep=true in the query string of the POST/PUT url please see an example using this in a many-to-many relation http://codepen.io/yariv/pen/eJQPEz

function updatePet(updatedPet) {
    $http({
      method: 'PUT',
      url: Backand.getApiUrl() + baseUrl + petsObjName + '/' + updatedPet.id,
     params: {
        deep: true,
        returnObject: true
     }, // to save the users array
     data: updatedPet
     }).then(function(response) {
     //$scope.readList(petsObjName);
    });
}

detailed answer here: How to create objects that have relationships when using Backand BaaS? related question Update a complex object on Backand using $http PUT

Community
  • 1
  • 1
user3375230
  • 421
  • 2
  • 5