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

Couldn't Get Records From Table

1 Answer 43 Views
Cloud Code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
İlkay
Top achievements
Rank 1
İlkay asked on 14 Jan 2016, 09:26 PM

Hi,

I'm newbie on cloud code of Telerik.

In User table afterCreate function, I want to query all data in a data table as written below. This simple code logs only line 7,16 and 17 (console.log(...)). The get function doesn't work. What's wrong with? There are only 16 record in 'Kategori' table.

 

01.Everlive.Events.afterCreate(function(request, response, context, done) {
02.         
03.    if(!response.error){
04.         
05.        var user = response.result.Id;
06.         
07.        console.log(user);
08.         
09.        var data = Everlive.Sdk.withMasterKey().data("KategoriSoruTablolari");
10.        data.get(null, function(datam) {
11.                console.log(1);
12.            }, function(err) {
13.                console.log(2);
14.            });
15.         
16.        console.log(3);
17.        console.log(4);
18.       
19.    }
20.    done();
21.});

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar Dimitrov
Telerik team
answered on 15 Jan 2016, 10:15 AM
Hello İlkay,

The issue that you faced is common, if you are new to the javascript asynchronous side. In the afterCreate cloud function, we provide you with the done callback in order to notify us when you have finished with the execution of your code. The usage of the done is mandatory and cannot be omitted. However putting it on the wrong place of your code may cause your code to finish earlier than it should be, just as it happens in your case.

data.get is asynchronous function and you should wait it to finish, before calling the done. Currently you are issuing a request to get your items and immediately after that you call done() which does not let your output "1" or "2" from the two callbacks. 

So all you have to do is move that call as follows:

Everlive.Events.afterCreate(function(request, response, context, done) {
          
    if(!response.error){       
        var user = response.result.Id;        
        console.log(user);        
        var data = Everlive.Sdk.withMasterKey().data("KategoriSoruTablolari");
        data.get(null, function(datam) {
               console.log(1);
                done();
           }, function(err) {
                console.log(2);
                done();
        });
 
        console.log(3);
        console.log(4);
    }
});



Regards,
Dimitar Dimitrov
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
Cloud Code
Asked by
İlkay
Top achievements
Rank 1
Answers by
Dimitar Dimitrov
Telerik team
Share this question
or