mcdonaldjosh
mcdonaldjosh

Reputation: 119

How to return Parse promise from cloud code module?

Would anyone know how to return the result of a Promise from a cloud code module? I am using the examples here but it keeps telling me that options is undefined (or nothing if I check with if(options) first.

I am calling the function with module.function as a promise, but still not getting results.

And ideas?

Edit: I can FORCE it to work but calling:

module.function({},{
 success:function(res){
  //do something
 }, 
 error:function(err){
  //handle error
 }
})

but this isn't really great since 1) I have to stick the empty object in there 2) I can't force the object to work like a promise and therefore lose my ability to chain.

Upvotes: 4

Views: 337

Answers (1)

danh
danh

Reputation: 62686

Not sure if the issue is with modules or promises. Here's some code that illustrates both, creating a module with a function that returns a promise, then calling that from a cloud function.

Create a module like this:

// in 'cloud/somemodule.js'

// return a promise to find instances of SomeObject
exports.someFunction = function(someValue) {
    var query = new Parse.Query("SomeObject");
    query.equalTo("someProperty", someValue);
    return query.find();
};

Include the module by requiring it:

// in 'cloud/main.js'
var SomeModule = require('cloud/somemodule.js');

Parse.Cloud.define("useTheModule", function(request, response) {
    var value = request.params.value;
    // use the module by mentioning it
    // the promise returned by someFunction can be chained with .then()
    SomeModule.someFunction(value).then(function(result) {
        response.success(result);
    }, function(error) {
        response.error(error);
    });
});

Upvotes: 4

Related Questions