I just migrated a Parse Server, and everything works, except cloud code. I have come to the understanding that it's because in my main.js I require the library "Underscore".
This is my cloud code function:
Parse.Cloud.define("ReadyUp", function(request, response) {
var _ = require('underscore');
var fbid = request.user.get("fbid");
var query = new Parse.Query("Spel");
query.equalTo("lobby", fbid);
query.find().then(function(results) {
_.each(results, function(spel) {
spel.addUnique("ready", fbid);
});
return Parse.Object.saveAll(results);
}).then(function(result) {
response.success(result);
}, function(error) {
response.error(error);
});
});
The code worked with no error before the migration. I'm guessing the require doesn't find the right folder. To give you folder structure it looks like this:
Cloudcode location: mainfolder->cloud->main.js
Underscore library: mainfolder->node_modules->underscore(folder)
Is the code faulty or is the structure of folders faulty?
Thanks in advance!
/Martin
You have to point to correct underscore file. I did the following:
var _ = require('../node_modules/underscore/underscore.js')
Add underscore to your dependencies in package.json
, either manually or run npm install underscore --save
This will result in a line like this:
"underscore": "^1.8.3"
From then, you can actually do this
var _ = require('underscore');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With