using Javascript SDK within CloudFunction not entering then() block

2 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    4 posts
    Member since:
    Jul 2016

    Posted 27 Aug Link to this post

    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();
    });

     

     

  2. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 29 Aug Link to this post

    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.
     
Back to Top