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

synchronous Everlive Query ?

7 Answers 195 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.
Max
Top achievements
Rank 1
Max asked on 30 Jan 2015, 02:15 PM
is there a way to do synchronous everlive requests using the Everlive SDK in cloudcode?

as there is no library support in cloud code we are not able to create own promises neither use the REST API.

promises would kinda work for me too as i need a function that several cloud codes have to call. i still would to have copy the same function into all, but at least the function would be separate of the actual requesting code.

issuing an ajax request for the REST API  would enable synchronous, however it requires jQuery.
(despite the problem of having to define API key and probably master key in cloud code which would also be bad i guess ...)

synchronous request would there ease this for me, but i could not find any information about it

7 Answers, 1 is accepted

Sort by
0
Accepted
Anton Dobrev
Telerik team
answered on 02 Feb 2015, 03:52 PM
Hi Max,

You can define a function which will be available in the current scope in the following way:
function retrieveMyItems() {
    var el = Everlive.Sdk.$;
 
    var filter = {}; // user-defined filter here
    var places = el.data("Places");
 
    return places.get(filter);
}

Then use this function in the Cloud Code events (or in a Cloud function) like:

function retrieveMyItems() {
    var el = Everlive.Sdk.$;
 
    var filter = {}; // user-defined filter here
    var places = el.data("Places");
 
    return places.get(filter);
}
 
Everlive.Events.beforeCreate(function(request, context, done) {
     retrieveMyItems().then(function(data) {
        var result = data.result;
        console.log(JSON.stringify(result));
        done();
    }, function(err) {
        console.log(JSON.stringify(err));
        done();
    });
});

I hope that this is helpful for your use case. You can add further functions and use the promises they return when resolving other promises.

In addition, please take a look into the introduction article about who to use the JavaScript SDK in the Cloud Code (or Cloud Functions) here.

Let me know if you have questions, I will be happy to help.

Best regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
Anton Dobrev
Telerik team
answered on 22 Jul 2015, 12:06 PM
Hello Max,

This is a quick announcement that you can now create your own promises in the Cloud Code for Data and Cloud Functions using the integrated RSVP library. In addition, the Underscore utility is also available for use within your custom code.

More information is available in the documentation article here.

Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
Anthony
Top achievements
Rank 1
answered on 06 Oct 2015, 07:05 AM

Hi Anton

Could you perhaps provide an example on how to use the RSVP library with a cloud code asynchronous request?

I am currently struggling to create a record for each item in a list, and catch any errors if they occur.
After all record created successfully, I want to run another function.

The below code wont work at all, but you will get an idea what I am trying to do.

Everlive.CloudFunction.onRequest(function(request, response, done){
  var ​members= request.data.Members;
  var index;
        for (index = 0; index < members.length; ++index) {
          everliveApp.data('​Member').create({'Surname': members[index].Surname},{},
          function(data)​{
              //when all records created successfully, do something else
              runFunctionAfterAllSuccess();
              done();}, 
          function(error){
              done();
           });
).
};

});

0
Anton Dobrev
Telerik team
answered on 07 Oct 2015, 09:54 AM
@Trevor

Indeed, running an asynchronous code into a for loop in the cloud code layer is not advised. In the current case you can use the multiple create option in the JavaScript SDK and supply the members array to the create method.

For instance:
Everlive.CloudFunction.onRequest(function(request, response, done) {
  // ommitted for brevity
 
    for (index = 0; index < membersCount; ++index) {
        var currentMember = {
            'Surname': members[index].Surname
        };
        createMembersArray.push(currentMember);
    }
 
    everliveApp.data('​Member').create(createMembersArray).then(
        function(data)​ {
            //when all records created successfully, do something else
            runFunctionAfterAllSuccess();
            done();
        },
        function(error) {
            // error occurred during creating members
            done();
        });
});

If you need to make a lot of single item operations and await their result:
Everlive.CloudFunction.onRequest(function(request, response, done){
  var rsvp = require('rsvp');
  var ​members= request.data.Members;
  var promises = [];
  for (var index = 0; index < members.length; ++index) {
    var createPromise = everliveApp.data('​Member').create({'Surname': members[index]);
    promises.push(createPromise);
  };
 
  rsvp.all(promises).then(function (res) {
    done();
  }, function (err) {
    done(err);
  })
});

Let me know if you have questions.

Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
Michael
Top achievements
Rank 1
answered on 09 Oct 2015, 04:18 PM

[quote]Anton Dobrev said:@Trevor
rsvp.all(promises).then(function (res) {
    done();
  }, function (err) {
    done(err);
  })
[/quote]

VERY HELPFULL

thanks ;-)

0
Anton Dobrev
Telerik team
answered on 13 Oct 2015, 08:28 AM
@Michael

Glad that this example helped you.

Let me know if you come up with some other useful scenarios or suggestions for such.

Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
Anthony
Top achievements
Rank 1
answered on 13 Oct 2015, 11:57 AM
Thanks!! It helped me with my issue
Tags
Cloud Code
Asked by
Max
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Anthony
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Share this question
or