Clifton Labrum
Clifton Labrum

Reputation: 14098

Share a Function Across Multiple Cloud Code Files

I want to have a utilities.js file that I require in my Parse Cloud Code that contains various functions that I share across my Cloud Code files.

So in utilities.js I would have:

function sheBear(){
  return "rawr rawr rawr"
}

...and in whatever.js I could have:

require('cloud/helpers/utilities.js')

Parse.Cloud.define("tester", function(request) {
  var she = sheBear()
  response.success(she)
})

...and get the return value of sheBear(). I can't get this to work (I get an error that sheBear() isn't defined). Is function sharing possible like this?

Upvotes: 1

Views: 304

Answers (1)

rickerbh
rickerbh

Reputation: 9913

To get the sheBear() function exposed from utilities.js, you'll need to alter it a little so it's exported as a module.

exports.sheBear = function() {
  return "rawr rawr rawr";
}

And then you can include in whatever.js as below:

var utilities = require('cloud/utilities.js');
var she = utilities.sheBear();

Source: Parse Documentation: https://www.parse.com/docs/js/guide#cloud-code-advanced-modules

Upvotes: 3

Related Questions