2

I am trying to use the Parse Javascript SDK in a node.js environment and I am completely baffled as to why I can't get this to work. I use Parse in a Swift iOS application, so I am familiar with Parse client code.

Here's what I'm trying to do:

var Parse = require('parse/node');

function ParseModule() {
  var self = this;

  self.initialize = function () {
    return new Promise(function(resolve, reject) {
        Parse.initialize("myAppID", "myJavascriptKey", "myMasterKey");
        Parse.serverUrl = "https://myserver.com/parse";
        Parse.Cloud.useMasterKey();

        var query = new Parse.Query("MyClassName");
        query.find()
            .then(
                function(results) {
                    console.log("yay! it worked");
                    resolve();
                }
            ).catch(
                function(err) {
                    output.log("parse error: " + err);
                    reject(err);
                }
            );
      });
  }

  return self;
}

module.exports = new ParseModule();

When my module's initialize function is called, after making the 'find' call, in node-debug it just hangs node. In node it just exits the application, so I get no error. This is probably because the wrong (hardcoded) URL to the server is being used.

By the way, I really do want to use the master key not the Javascript key but the initialize method requires the Javascript key.

Also, when I step through the Parse SDK code, I get to this statement:

    var url = _CoreManager2['default'].get('SERVER_URL');

in RESTController.js. The server url that it's finding if from the 'config' object which seems to me to be the one used by parse-server. Thus, setting the serverUrl as instructed by the docs has no effect.

Am I not using the correct Parse NPM module? I'm new to node.js.

I'm wasting my entire personal project development day on this!!

Thanks, Peter...

UPDATE: I've added the following line of code after the (almost definitely useless) set of the Parse.serverUrl=... . This seems to fix the issue.

Parse.CoreManager.set('SERVER_URL', "https://myserver.com/parse");

However, this seems like a hack to me. I would appreciate it if someone who is doing something similar to what I'm doing would tell me what I'm doing wrong (i.e. the right way to do this).

1
  • Parse.Cloud.useMasterKey(); is not work. You should query.find({useMasterKey:true} ) instead. Commented May 31, 2016 at 13:04

1 Answer 1

1

after making the 'find' call, in node-debug it just hangs node. In node it just exits the application, so I get no error.

There are 2 points. First, Parse.Promise is not es6-like promise, and it do not have catch function. https://parse.com/docs/js/api/classes/Parse.Promise.html

Second, I have encounter lots of problems due to js sdk catch the exception. And I can't find it out. In your code, output is not defined, so there is a missing ReferenceError from output.log.

Try below code.

var Parse = require('parse/node');

function ParseModule() {
  var self = this;
  self.initialize = function () {
    return new Promise(function(resolve, reject) {

        Parse.initialize("myAppID", null, "myMasterKey");
        Parse.serverUrl = "http://localhost:1337/parse";
        //Parse.Cloud.useMasterKey();
        console.log(1);
        var query = new Parse.Query("MyClassName");
        query.find({useMasterKey:true}).then(
            function(results) {
                console.log(2);
                console.log("yay! it worked");
                resolve();
            },function(err) {
                try{
                    console.log(3);
                    output.log("parse error: " + err);
                    reject(err);
                } catch(error){
                    console.log(4);
                    console.log(error);
                }
            }
        );

    });
  }

  return self;
}

module.exports = new ParseModule();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply @ChunTingLin. Yes, after posting this, I realized that catch was not going to work with Parse Promises and corrected that. A true Javascript/Node newbie error.
There is a short-coming of parse javascript sdk. It will catch some error, so that you cannot find where the problem is. I'm suffer this for many times XD.
Thank you, I had the same problem, and it was driving me batshit crazy. From the Parse source code, I found the following: ``` set serverURL(value) { _CoreManager.default.set('SERVER_URL', value); }, ``` It looks like a member, but if you look closely it is not setServerURL, but set<space>serverURL. It looks like a typo, but it seems to mean something. I can find no reference to tell me what.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.