Everlive.Sdk.$.data not working in Cloud Function

3 posts, 1 answers
  1. Arpita
    Arpita avatar
    5 posts
    Member since:
    Mar 2015

    Posted 29 Apr 2015 Link to this post

    I am new to Telerik platform backend services. I am trying to access cloud data in cloud function using Everlive.Sdk. I followed the Api Using Javascript Sdk in telerik documentation to get data. Everything is working fine without any error but instead of getting data I get blank response (like this: {}) when I Execute the cloud function. I added console.log statements in my code but I noticed that callback function in Everlive.Sdk.$.data(null, function(error, data)){} was not called. My code is as followed:

    Everlive.CloudFunction.onRequest(function(request, response, done){
        console.log("onrequest called..");
         
         Everlive.Sdk.$.data('questions').get(null, // filter
            function(error, data) {
                console.log("in callback function...");
                if (error) {
                     console.log("in error...");
                    // When using cloud functions you need to set the result like so:
                    response.body = error;
                    response.statusCode = 500;
                } else {
                     console.log("in success..");
                    // When using cloud functions you need to set the result like so:
                    response.body = JSON.stringify(data.result);
                }
                done();
         });
        done();
    });

    Please help me with this, I am kind of stucked here. Let me know what I am missing.

    Thanks & Regards

    Arpita

  2. Answer
    Yosif
    Admin
    Yosif avatar
    9 posts

    Posted 30 Apr 2015 Link to this post

    Hello Arpita ,

    The reason for this is that you are invoking done() right after you invoke the .get() function for the data type. As Everlive.Sdk.$.data('questions').get() is an asynchronous function you have to call the done() in its callback(s), because now you are immediately ending the CloudFunction without waiting for the .get() to finish.

    If you remove the done() as shown below everything else will work great. 

    Everlive.CloudFunction.onRequest(function(request, response, done){
        console.log("onrequest called..");
         
         Everlive.Sdk.$.data('questions').get(null// filter
            function(error, data) {
                console.log("in callback function...");
                if (error) {
                     console.log("in error...");
                    // When using cloud functions you need to set the result like so:
                    response.body = error;
                    response.statusCode = 500;
                else {
                     console.log("in success..");
                    // When using cloud functions you need to set the result like so:
                    response.body = JSON.stringify(data.result);
                }
                done();
         });
         // done(); -This done() is causing the problem, as it returns immediately, but the request hasn't completed yet.
    });

    I hope this helps.

    Regards,
    Pavel
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  3. Arpita
    Arpita avatar
    5 posts
    Member since:
    Mar 2015

    Posted 30 Apr 2015 in reply to Yosif Link to this post

    Hello Pavel,

    Thanks alot for the solution. It worked for me.

    Thanks & Regards,

    Arpita

Back to Top