问题
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
回答1:
You have to point to correct underscore file. I did the following:
var _ = require('../node_modules/underscore/underscore.js')
回答2:
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');
来源:https://stackoverflow.com/questions/35382451/cant-get-underscore-to-work-with-parse-server