Parse-server: Unable to call Parse Cloud functions after hosting on Heroku XMLHttpRequest failed: "Unable to connect to the Parse API"

Created on 23 Jan 2017  Â·  18Comments  Â·  Source: parse-community/parse-server

So I am sure the cloud function works because I am able to hit the function by using curl.
This is what I ran and it works.

curl -X POST -H "X-Parse-Application-Id: myAppId" -H "Content-Type: application/json" -d '{}' https://xxxx.herokuapp.com/parse/functions/upsert

Here is my client configuration code

import Parse from 'parse/react-native';
Parse.initialize('myAppId');
Parse.serverURL = 'https://xxxx.herokuapp.com/parse/';

I am getting a 100: XMLHttpRequest failed: "Unable to connect to the Parse API" error most likely due to issues with https

Please let me know what I can do to fix this problem. Thanks!

Most helpful comment

+1
I might be experiencing a similar issue. I just updated my parse-server and parse js sdk that is being hosted on Heroku, and now all my cloud code is failing. It works up until I have to query the database for something and then it throws this error 'XMLHttpRequest failed: "Unable to connect to the Parse API"'.

All 18 comments

@willyyang how are you initializing the server? Can you share the code? Are you configuring the serverURL property?

@NatanRolnik server is using an environment variable set on heroku https://xxxx.herokuapp.com/parse/ , along with the other variables seen below

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

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

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

console.log('serverURL')
console.log(process.env.SERVER_URL)

// Video guide
//https://github.com/ParsePlatform/parse-server
var api = new ParseServer({
  databaseURI: databaseUri || environment.default.databaseURI,
  cloud: process.env.CLOUD_CODE_MAIN || path.join(__dirname, '/cloud/main.js'),
  appId: process.env.APP_ID || environment.default.appId,
  masterKey: process.env.MASTER_KEY || environment.default.masterKey, //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || environment.default.serverURL,  // Don't forget to change to https if needed

  javascriptKey: process.env.JAVASCRIPT_KEY || '',  //** add this line no need to set values, they will be overwritten by heroku config vars
  restAPIKey: process.env.REST_API_KEY || '', //** add this line
  dotNetKey: process.env.DOT_NET_KEY || '', //** add this line
  clientKey: process.env.CLIENT_KEY || '', //** add this line

  liveQuery: {
    classNames: ['Donations', 'ActiveConnections'] // List of classes to support for query subscriptions
  }
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

var app = express();
app.use(cors());

// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));

// Serve the Parse API on the /parse URL prefix
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!');
});

// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
  res.sendFile(path.join(__dirname, '/public/test.html'));
});

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 + '.');
});

// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

The server is initialized correctly , I believe, since I am able to hit the endpoints using curl commands

When calling from the client, does it work if you add the clientKey:
Parse.initialize('myAppId', 'yourClientKey');

does not work.

Did you try with javascriptKey instead?

I've removed the keys cus they aren't necessary . And curl still works .
On Mon, 23 Jan 2017 at 9:31 AM Natan Rolnik notifications@github.com
wrote:

Did you try with javascriptKey instead?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ParsePlatform/parse-server/issues/3420#issuecomment-274502282,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGGZYRcPvyNRlCUgjpclPpL4TU9DpiDzks5rVLmygaJpZM4LqjeV
.

If curl works, there is no reason why the call from React Native won't, from the server perspective.

It's because of heroku enabling SSL . There's a bug with this client that
doesn't deal with that.
On Mon, 23 Jan 2017 at 11:09 AM Natan Rolnik notifications@github.com
wrote:

If curl works, there is no reason why the call from React Native won't.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ParsePlatform/parse-server/issues/3420#issuecomment-274531064,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGGZYW8uaFOn9RO2VPoDfduoNNO9ORE1ks5rVNC7gaJpZM4LqjeV
.

Can you provide us more information? Which platform and OS are you running React native on? Can you print some logs?
Maybe a sample project?

+1
I might be experiencing a similar issue. I just updated my parse-server and parse js sdk that is being hosted on Heroku, and now all my cloud code is failing. It works up until I have to query the database for something and then it throws this error 'XMLHttpRequest failed: "Unable to connect to the Parse API"'.

Hosted on AWS EBS and it works fine. It's an issue with the enabled SSL.

On Wed, Jan 25, 2017 at 1:17 PM, Peter Zverkov notifications@github.com
wrote:

Hi guys,

You can try to use fcm-node:

From Cloud part:

var SERVER_KEY_FCM = 'AIza...';
var FCM = require('fcm-node');
var fcm = new FCM(SERVER_KEY_FCM);

Parse.Cloud.define("CloudPushFCM", function (request, responseTotal) {

var params = request.params;
var gcmToken = params.gcmToken;

var message = { //this may vary according to the message type single recipient, multicast, topic, etc
to: gcmToken,
// collapse_key: 'your_collapse_key',
priority: "high",

notification: {
    title: 'Title of your push notification',
    body: 'Body of your push notification',
    icon: 'ic_stat_utn',
    tag: 'tag text'
},

data: {  //you can send only notification or only data(or include both)
    my_key: 'my value',
    my_another_key: 'my another value'
}

};

fcm.send(message, function(err, response){
if(err) responseTotal.error("error with sendPush: " + err);
else responseTotal.success("Push send");
}, {useMasterKey: true});

});

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ParsePlatform/parse-server/issues/3420#issuecomment-275188366,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGGZYbwD5Sdhmu2zwXQlHLJgb5EB2CZVks5rV5HWgaJpZM4LqjeV
.

it's causing an issue with this library..

On Wed, Jan 25, 2017 at 1:19 PM, Will Yang willyyang604@gmail.com wrote:

Hosted on AWS EBS and it works fine. It's an issue with the enabled SSL.

On Wed, Jan 25, 2017 at 1:17 PM, Peter Zverkov notifications@github.com
wrote:

Hi guys,

You can try to use fcm-node:

From Cloud part:

var SERVER_KEY_FCM = 'AIza...';
var FCM = require('fcm-node');
var fcm = new FCM(SERVER_KEY_FCM);

Parse.Cloud.define("CloudPushFCM", function (request, responseTotal) {

var params = request.params;
var gcmToken = params.gcmToken;

var message = { //this may vary according to the message type single recipient, multicast, topic, etc
to: gcmToken,
// collapse_key: 'your_collapse_key',
priority: "high",

notification: {
    title: 'Title of your push notification',
    body: 'Body of your push notification',
    icon: 'ic_stat_utn',
    tag: 'tag text'
},

data: {  //you can send only notification or only data(or include both)
    my_key: 'my value',
    my_another_key: 'my another value'
}

};

fcm.send(message, function(err, response){
if(err) responseTotal.error("error with sendPush: " + err);
else responseTotal.success("Push send");
}, {useMasterKey: true});

});

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ParsePlatform/parse-server/issues/3420#issuecomment-275188366,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGGZYbwD5Sdhmu2zwXQlHLJgb5EB2CZVks5rV5HWgaJpZM4LqjeV
.

I think I figured something out. At the top of the main.js in cloud code, I put Parse.serverURL = process.env.SERVER_URL where SERVER_URL is the heroku url and it seems to work now.

@GGross5213 : Was the URL https or http? I added the URL in serverURL, and it still doesn't seem to want to work. Any ideas/thoughts?

@GGross5213 same question as @johndoehelloworld. Would be amazing if you still have it handy!

@jbschaff @johndoehelloworld I have been looking to see what I did and I am having trouble finding it. I believe the URL was https.

@GGross5213 thanks, I was able to sort it out. Sent a test notification through cloud code function and was successfully delivered. In Heroku config vars I have SERVER_URL set to: https://xxxxxxxx.herokuapp.com/parse and had to modify index.js with: var https = require('https'); and also Parse.serverURL = process.env.SERVER_URL; before var api = new ParseServer({. Also set serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carjo422 picture carjo422  Â·  3Comments

darkprgrmmr picture darkprgrmmr  Â·  4Comments

ShawnBaek picture ShawnBaek  Â·  4Comments

sanergulec picture sanergulec  Â·  4Comments

jaydeep82 picture jaydeep82  Â·  4Comments