adding related data to backand api

谁说我不能喝 提交于 2019-12-02 06:46:00

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/37672602/adding-related-data-to-backand-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!