Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Unable to connect to the Parse API" error thrown when trying to call a Cloud function with a Parse Query. #817

Closed
guptasachin25 opened this issue Mar 3, 2016 · 17 comments

Comments

@guptasachin25
Copy link

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

-[X] You've met the prerequisites.

-[X] You're running the latest version of Parse Server.

-[X] You've searched through existing issues. Chances are that your issue has been reported or resolved before.

Environment Setup

Running Parse Server on Localhost.
Downloaded code from Amazon Elastic Beanstalk

Steps to reproduce

Running any cloud function with a query. Normal cloud function works well. Called using node script.

Logs/Trace

I found other issue #651 (comment) which is currently closed. However, even after following the steps and setting up serverURL problem doesn't resolve. XHR failed with status 0.

@guptasachin25
Copy link
Author

@gfosco you worked on previous issues. Can you help resolve this one.

@gfosco
Copy link
Contributor

gfosco commented Mar 3, 2016

Can you provide the code for your function? (or the whole cloud code file...)

@gfosco
Copy link
Contributor

gfosco commented Mar 4, 2016

Discussed over email, problem is the script is overwriting the Parse global which definitely breaks everything.

@gfosco gfosco closed this as completed Mar 4, 2016
@w3care25
Copy link

I am using self hosted parse-server and not able to use cloud functions since I have enabled HTTPS, if I revert it back to HTTP it works as expected.

Also he problem is only with those cloud function which have Parse.Query, if i create a function simply return "Hello" as response it works on both HTTP and HTTPS.

I am getting following error message when run a cloud function which have Parse.Query code.

{
"code": 141,
"error": "XMLHttpRequest failed: "Unable to connect to the Parse API"--100"
}

can anyone tell me how can I fix this?

@chandanthakur
Copy link

I am also facing the same issue. Any pointers with that ? w3care25, did you end up fixing it ?

@ckarmy
Copy link

ckarmy commented Aug 4, 2016

I am using self hosted parse-server and not able to use cloud functions since I have enabled HTTPS, if I revert it back to HTTP it works as expected.

Also he problem is only with those cloud function which have Parse.Query, if i create a function simply return "Hello" as response it works on both HTTP and HTTPS.

I am getting following error message when run a cloud function which have Parse.Query code.

I have the same problem!! Any way to solve this??

@jova-dev
Copy link

+1

@w3care25
Copy link

@chandanthakur You need to add process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; in you instance file of parse server if you are running into similar issue

@w3care25
Copy link

@ckarmy You need to create an instance with SSL something like below:

_var privateKey = fs.readFileSync( 'path to ssl key ( .key ) file' );
var certificate = fs.readFileSync( 'path to ssl crt ( .crt ) file' );
var cabundles = fs.readFileSync( 'path to ssl cabundle ( .cabundle ) file');

https.createServer({
key: privateKey,
cert: certificate,
ca: cabundles
}, app).listen(port);_

Don't forgot to add var https = require('https');

@willyyang
Copy link

@w3care25 adding in the process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" line still doesn't fix it. Any other ideas? thanks.

@w3care25
Copy link

w3care25 commented Sep 22, 2016

If you have configured everything correctly. You may need to restart your server after adding this line.

If you are still having this after server restart, Could you please share you instance file code ( after replcing your confidential credentails with dummy details)?

@willyyang
Copy link

@w3care25 I have restarted the server but I am still getting the same results.

// SERVER_URL is set to https://APPNAME.herokuapp.com/parse through Config Variables
// MONGODB_URI is set through Config Variables

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;

if (!databaseUri) {
  console.log('DATABASE_URI not specified, falling back to localhost.');
}

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'xxx',
  masterKey: process.env.MASTER_KEY || 'xxx', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed
  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  }
});

var app = express();
app.use('/public', express.static(path.join(__dirname, '/public')));
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

//Required to receive JSON body from POST
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
  res.status(200).send('I dream of being a website.  Please star the parse-server repo on GitHub!');
});

// This will enable the Live Query real-time server
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
    console.log('parse-server-example running on port ' + port + '.');
});
ParseServer.createLiveQueryServer(httpServer);

Hitting the sample hello function works but all of mine return the XMLHttpRequest failed: \"Unable to connect to the Parse API\"--100 error.

Thank you very much for your help.

@w3care25
Copy link

w3care25 commented Sep 22, 2016

backup you instance file and try following code

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var app = express();
var fs = require('fs');

var api = new ParseServer({
databaseURI: 'mongodb://localhost:27017/dev', // Connection string for your MongoDB database
cloud: '/home/myApp/cloud/main.js', // Absolute path to your Cloud Code
appId: 'myAppId',
masterKey: 'myMasterKey', // Keep this key secret!
fileKey: 'optionalFileKey',
serverURL: 'http://localhost:1337/parse' // Don't forget to change to https if needed
});

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// Serve the Parse API on the /parse URL prefix
app.use('/parse', api);

app.listen(1337, function() {
console.log('parse-server-example running on port 1337.');
});

Make sure you are not accessing you sever with https (SSL enabled). If you need to access your server with https then you will have to add following

var https = require('https');

var privateKey = fs.readFileSync( 'path to ssl.key' );
var certificate = fs.readFileSync( 'path to ssl.crt' );
var cabundles = fs.readFileSync( 'path to ssl.cabundle');

https.createServer({
key: privateKey,
cert: certificate,
ca: cabundles
}, app).listen(1337);

and remove

app.listen(1337, function() {
console.log('parse-server-example running on port 1337.');
});

@willyyang
Copy link

@w3care25 Im hosting this on heroku. Wouldn't serverURL: 'http://localhost:1337/parse' and databaseURI: 'mongodb://localhost:27017/dev' point to my local ?

@w3care25
Copy link

You can use Heroku credentials here

@w3care25
Copy link

@willyyang were you able to fix your issue ?

@willyyang
Copy link

@w3care25 I'm looking for another alternative still because I don't to buy a certificate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants