Start a new topic

Execute 2 rest api calls with promises and execute action when done..

Hi,



i'm following the example of http://devcenter.kinvey.com/nodejs/guides/concepts



what i have on my server:



code:

getProject(req.body.projectId).then(function(project) {

console.log(project);



return getInfo(req.body.projectId);

}).then(function(info)

{

// doesn't get here

console.log(info);

console.log(project);

// execute code when both are done AND GET DATA OF BOTH(?)

},

function(anyError)

{

console.log(anyError);

});

getProject returns: return Kinvey.DataStore.get('Project', projectId);

getInfo returns: return Kinvey.DataStore.find('Info', query);



However it only executes the getproject succesfully and the getinfo doesn't return anything?

I also need the data of both request in my function, but i don't understand why this isn't working?



Thanks in advance

Hey Jonas, Sometimes I've seen issues like this resolve when you try them out in the API console to debug.
Is there any error logged? If getProject returns an error, getInfo will never be executed.



By the way, the console.log(project) in the second handler will not log anything, since the variable project is not defined in that handler, but in the first one.
Just for testing and clearer logging you can try using the "non-promise-like" way:

Just put the second call in the success function of the first one and log anything you can.

Like

var promise = Kinvey.DataStore.find('collection', query, {

success: function(resp) {

console.log(resp)

var secondPromise = Kinvey.DataStore.save('collection', entity, {

success: function(secondResponse) {

console.log(secondResponse);

},

error: function(secondResponse) {

console.log(secondResponse);

}

});

},

error: function(resp) {

console.log(resp)

}

})



That way you can see easier where the error happens. After everything runs smoothly use promises with the same pieces of code.
Login or Signup to post a comment