5

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

Martin Kjellberg
  • 615
  • 1
  • 6
  • 22

2 Answers2

4

You have to point to correct underscore file. I did the following:

var _ = require('../node_modules/underscore/underscore.js')
M.T
  • 4,917
  • 4
  • 33
  • 52
Trong Vu
  • 91
  • 8
1

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');
Rene Pot
  • 24,681
  • 7
  • 68
  • 92