This is a migrated thread and some comments may be shown as answers.

using Javascript SDK within CloudFunction not entering then() block

1 Answer 47 Views
JavaScript SDK
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jonathan
Top achievements
Rank 1
Jonathan asked on 27 Aug 2016, 06:59 AM

Hi,

I am trying to use the Everlive SDK within a Cloud Function and am having some trouble with what seems that the then() block is not entering. For example see this piece of code, where the first console.info works fine, but the console.log in the then() block does not show nor is the response body returned.

Probably I am missing something that is related to using the SDK within a Cloud Function. 

Any advice?

Thanks!

 

Everlive.CloudFunction.onRequest(function(request, response, done) {
  console.info(JSON.stringify(request.data)); // works fine
  var availability = Everlive.Sdk.$.data('Availability');
  var existingContactsFilter = new Everlive.Sdk.Query();
  existingContactsFilter.select('username').where().isin('username', ['user1', 'user2']);
  availability.get(existingContactsFilter).then(function(data){
    console.log(JSON.stringify(data)); // doesn't show up
    response.body = JSON.stringify(request.data); // nor is this response returned
  }, function(error){
    console.log(JSON.stringify(error));
  });
  done();
});

 

 

1 Answer, 1 is accepted

Sort by
0
Anton Dobrev
Telerik team
answered on 29 Aug 2016, 01:17 PM
Hi Jonathan,

The reason is that the async call to the Availability type is not awaited. This said, when the parser and code executor reaches the done the function is completed without awaiting the result.

To change this call done when you are ready processing the result from the call to Availability:


Everlive.CloudFunction.onRequest(function(request, response, done) {
    console.info(JSON.stringify(request.data)); // works fine
    var availability = Everlive.Sdk.$.data('Availability');
    var existingContactsFilter = new Everlive.Sdk.Query();
 
    existingContactsFilter.select('username').where().isin('username', ['user1', 'user2']);
 
    availability.get(existingContactsFilter).then(function(data) {
        console.log(JSON.stringify(data)); // doesn't show up
        response.body = JSON.stringify(request.data); // nor is this response returned
 
        done();
    }, function(error) {
        console.log(JSON.stringify(error));
 
        done();
    });
});


Regards,
Anton Dobrev
Telerik by Progress
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
JavaScript SDK
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Share this question
or