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

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

2 Answers 114 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Arpita
Top achievements
Rank 1
Arpita asked on 29 Apr 2015, 12:57 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Yosif
Telerik team
answered on 30 Apr 2015, 01:23 PM
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.
 
0
Arpita
Top achievements
Rank 1
answered on 30 Apr 2015, 01:43 PM

Hello Pavel,

Thanks alot for the solution. It worked for me.

Thanks & Regards,

Arpita

Tags
General Discussion
Asked by
Arpita
Top achievements
Rank 1
Answers by
Yosif
Telerik team
Arpita
Top achievements
Rank 1
Share this question
or