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

ⅰ亾dé卋堺 提交于 2019-12-10 14:33:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!