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
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
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
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();
});).
};
});
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
[quote]Anton Dobrev said:@Trevor
rsvp.all(promises).then(function (res) {
done();
}, function (err) {
done(err);
})
[/quote]
VERY HELPFULL
thanks ;-)
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