0

When I save new data to a DataStore in Angular, I don't want to specify the _id. The system automatically assigns one. From looking at the network trace, the _id is passed back to the application in the response - https://baas.kinvey.com/appdata/appxxx/activities/54ac3d8671e2d7933b0116b4 - but I don't see anyway of finding that in the Angular documentation about how to retrieve that _id so I can add it to an existing list or do other processing.

    var promise = $kinvey.DataStore.save('activities', {
                text : $scope.newActivity.text ,
                duedate : '2015-01-01'
            });
         promise.then(
             function () {
                 $scope.newActivity.text = '';
             },
             function (error) {
                 //Kinvey insert finished with error
                 alert("Error insert: " + JSON.stringify(error));
             });
jhoskins98
  • 114
  • 5

1 Answers1

0

Kinvey will actually return the object to you in the promise, and you can just grab the _id off the returned object.

promise.then(function(data) {
    // Here's where you get your _id
    $scope.newActivity.text = data._id;
}, function(err) {
    console.error(err);
});
  • Thanks That did it but... Where the heck would I have found this? – jhoskins98 Jan 15 '15 at 18:30
  • Sorry for the slow response, @jhoskins98 ! Looks like Kinvey attempts to explain this on their [Concepts](http://devcenter.kinvey.com/angular/guides/concepts) page, but I don't think it's super clear on there. I've found Todd Motto's [AngularJS tutorial](https://www.airpair.com/angularjs/posts/angularjs-tutorial) to be excellent when I was getting started. My project at work uses promises so heavily that I got used to the paradigm pretty quick. Also look into chaining promises, which can be very useful—albeit a little tricky to make sure the right data falls into the right promise. – Michael Baxter Feb 07 '15 at 19:52