Couldn't Get Records From Table

2 posts, 1 answers
  1. İlkay
    İlkay avatar
    6 posts
    Member since:
    Jul 2015

    Posted 14 Jan 2016 Link to this post


    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) {
    03.    if(!response.error){
    05.        var user = response.result.Id;
    07.        console.log(user);
    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.            });
    16.        console.log(3);
    17.        console.log(4);
    19.    }
    20.    done();
  2. Answer
    Dimitar Dimitrov
    Dimitar Dimitrov avatar
    26 posts

    Posted 15 Jan 2016 Link to this post

    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) {
            var user = response.result.Id;        
            var data = Everlive.Sdk.withMasterKey().data("KategoriSoruTablolari");
            data.get(null, function(datam) {
               }, function(err) {

    Dimitar Dimitrov
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
Back to Top