Parse-server: You need to call Parse.initialize before using Parse

Created on 7 Mar 2016  路  2Comments  路  Source: parse-community/parse-server

I am trying to migrate my Parse application to my local computer as backend instance and using MongoLab MongoDB. I have downloaded https://github.com/ParsePlatform/parse-server-example and configured with my appId, masterKey, databaseURI, etc. as follows :

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://fuatcoskun:[email protected]:19268/database',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'appId',
  masterKey: process.env.MASTER_KEY || 'masterKey', //Add your master key here. Keep it secret!
  clientKey: process.env.CLIENT_KEY || 'clientKey', //Add your client key here.
  restAPIKey: process.env.REST_KEY || 'restApiKey',
  serverURL: process.env.SERVER_URL || 'http://localhost:1337'  // Don't forget to change to https if needed
});

After running "npm start" my backend service starts to listen port 1337 :

> [email protected] start /Users/fuat/parseLocal/parse-server-example
> node index.js
parse-server-example running on port 1337.

I have a huge code base but I added a dummy function for easing basic check before calling actual functions.

Parse.Cloud.define("deneme", function(req,res) {
    res.success("result : " + Constants.LIMIT);
});

I can call this function via following curl command:

`curl -X POST 
-H "X-Parse-Application-Id: appId"
-H "X-Parse-REST-API-Key: restApiKey" 
-H "Content-Type: application/json"
-d '{}' http://localhost:1337/parse/functions/deneme`

Result is successful :
{"result":"result : 10"}

Problem is that, I am getting the following error when I call some of my functions which make some stuff with ParseQuery class like querying database etc.

Uncaught internal server error. [Error: You need to call Parse.initialize before using Parse.] Error: You need to call Parse.initialize before using Parse.
    at Object.generatePath (/Users/fuat/parseLocal/parse-server-example/node_modules/parse/lib/node/Storage.js:82:13)
    at Object.currentInstallationId (/Users/fuat/parseLocal/parse-server-example/node_modules/parse/lib/node/InstallationController.js:43:37)
    at Object.request (/Users/fuat/parseLocal/parse-server-example/node_modules/parse/lib/node/RESTController.js:187:35)
    at Object.find (/Users/fuat/parseLocal/parse-server-example/node_modules/parse/lib/node/ParseQuery.js:1141:27)
    at ParseQuery.find (/Users/fuat/parseLocal/parse-server-example/node_modules/parse/lib/node/ParseQuery.js:288:25)
    at Object.MatchMaking.randomUsers (/Users/fuat/parseLocal/parse-server-example/cloud/matchMaking/MatchMaking.js:36:14)
    at MatchMaking.getRandomOpponentForTheFirstGameAPI (/Users/fuat/parseLocal/parse-server-example/cloud/matchMaking/MatchMaking.js:80:25)
    at /Users/fuat/parseLocal/parse-server-example/node_modules/parse-server/lib/Routers/FunctionsRouter.js:87:11
    at new Promise (/Users/fuat/parseLocal/parse-server-example/node_modules/parse-server/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:186:7)
    at handleCloudFunction (/Users/fuat/parseLocal/parse-server-example/node_modules/parse-server/lib/Routers/FunctionsRouter.js:81:16)

At Storage.js:82:13 there is APPLICATION_ID check as shown below :

if (!_CoreManager2['default'].get('APPLICATION_ID')) {
   throw new Error('You need to call Parse.initialize before using Parse.');
}

I was expecting that APPLICATION_ID, MASTER_KEY and similar parameters I defined with ParseServer instance should already be set. If I call

Parse.initialize("appId", "javascriptKey", "masterKey");

just top of my function on cloud code, it works as expected without error. But this is not a good workaround. How can I handle this difficulty? What should I do to initialize Parse successfuly before my function calls?

Additionally the same code base is working on Parse cloud without an extra Parse.initialize(..) call on top of functions. Most probably there is a missing initialization step in the https://github.com/ParsePlatform/parse-server-example/blob/master/index.js

Most helpful comment

In your cloud code, remove any definition of Parse. Do not require parse. It is pre-initialized and available to you as a Global and must not be re-defined.

All 2 comments

In your cloud code, remove any definition of Parse. Do not require parse. It is pre-initialized and available to you as a Global and must not be re-defined.

Thanks for you reply, but I am getting this error if I try to use "Parse" directly without "require(parse/node).Parse" definition.

Users.PrivateUserClass = Parse.Object.extend("PrivateUser");
                                                    ^
TypeError: Cannot read property 'Object' of undefined
    at Object.<anonymous> (/Users/fuat/parseLocal/parse-server-example/cloud/user/Users.js:49:31)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/Users/fuat/parseLocal/parse-server-example/cloud/main.js:1:75)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanergulec picture sanergulec  路  4Comments

pulse1989 picture pulse1989  路  3Comments

kilabyte picture kilabyte  路  4Comments

okaris picture okaris  路  4Comments

darkprgrmmr picture darkprgrmmr  路  4Comments