Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get 'underscore' to work with parse server

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

like image 666
Martin Kjellberg Avatar asked Feb 13 '16 16:02

Martin Kjellberg


2 Answers

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

var _ = require('../node_modules/underscore/underscore.js')
like image 176
Trong Vu Avatar answered Nov 17 '22 07:11

Trong Vu


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');
like image 1
Rene Pot Avatar answered Nov 17 '22 08:11

Rene Pot