Start a new topic

Relational data with an array.

My data model is as such:



topic: {

links: [ { ... } ]

}



I want to store links using the relational stuff with Kinvey. Saving a single link with the following works just fine, but it fails when I pass an array into it. According to the docs, this should work? ("This includes embedding references in array-valued properties.")



Code:



var updatedTopic = {

_id: $scope.topic._id,

name: $scope.topic.name,

day_offset: $scope.topic.day_offset,

kickstart_id: $scope.topic.kickstart_id,

overview: $scope.topic.overview,

topic_tpl_id: $scope.topic.topic_tpl_id,

files: $scope.topic.files,

links: $scope.topic.links (an array with one object, this works ... but fails with multiples)

};



// save the updated topic with the updated link

$kinvey.DataStore.save('topics', updatedTopic, {

relations : { links: 'topic-links' }

}).then(function(response) {

console.log(response);

})



When I pull down topic, it contains the embedded references to links properly (when there's only one entry in the array).



Do I need to iterate over my array and do API calls for each one? Also, is there a way to save an entity without have to "re-save" all of the properties?

Yes, the [latest version](http://devcenter.kinvey.com/angular/downloads) of the angular library now uses the built-in stringifier, so you no longer should run into this.
Thanks for sharing your answer Dave! Solved the problem for me as well.



Mark, you guys prob know this already, that there is a built-in JSON stringify function in Angular: https://docs.angularjs.org/api/ng/function/angular.toJson



This will clean up the $$hashkey used by Angular. In case you guys are thinking about patching the official Kinvey Angular library.
Yes, we are aware of the `$$hashKey` problems, for now it is best to strip them out yourself before saving, as you already figured out.
Yep, that was it. Just for future reference for peeps using angular.



links: angular.copy($scope.topic.links) // get rid of $$hashKey before saving relational data.
Yes, try renaming those fields without the $$.
Fetching works fine for one object, but not for multiples.
Here's the JSON.stringify($scope.topics.links):

https://gist.github.com/dmackerman/9843012



The first object is a KinveyRef that was successfully saved. Is it the $$hashKey things?

Can you provide the output of $scope.topic.liinks? I believe this should work just fine, but I'm guessing the array of objects you think is being sent isn't being sent quite right. I would try taking that array and turning it into a JSON string, see how it looks and include that here.



Also, are you just having a problem saving or fetching too? Try creating the kinveyRef array in the API console, and then fetch it from your application.
I get a 500 back from the server.



{"error":"KinveyInternalErrorRetry","description":"The Kinvey server encountered an unexpected error. Please retry your request","debug":"This operation does not support using both operators (beginning with a $) and non-operators at the same time. Please include either only operators or only non-operators in your request and try again."}



Also, can we get a better code formatting option?
Login or Signup to post a comment