Ben Anderson
Ben Anderson

Reputation: 1069

CORS error when using Back4App Parse Cloud Code Function

I have a Parse backend running on Back4App. They have a cloud code capability that lets you put NodeJs like functions that you can call from the javascript on your site. I'm not that familiar with NodeJs so there may be something wrong with my function syntax.

The NodeJs function needs to call the mailgun REST API, and I call the Parse cloud code function from Coffee Script on my site. I created my function by getting the REST call to mailgun working on Postman and then using Postman's code generation to generate a NodeJs function.

The problem is when I call the Parse cloud code function from my website I get the following errors:

[Error] Origin http://localhost:9000 is not allowed by Access-Control-Allow-Origin.
[Error] Failed to load resource: Origin http://localhost:9000 is not allowed by Access-Control-Allow-Origin. (sendemail, line 0)

I get this error whether it's from my local testing site or from my hosted testing site.

Here is the cloud code function on the Back4App parse server:

    Parse.Cloud.define("sendemail", function(request, response) {
    var request = require("request");

    var options = { method: 'POST',
      url: 'https://api.mailgun.net/v3/mg.mysite.com/messages',
      headers: 
      { 'Access-Control-Allow-Origin':'*',
        'Access-Control-Allow-Headers':'X-Requested-With',
        'Access-Control-Allow-Headers':'Content-Type',
        'Postman-Token': 'token',
        'cache-control': 'no-cache',
        Authorization: 'Basic <auth_token>',
        'Content-Type': 'application/x-www-form-urlencoded',
        'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },
      formData: 
      { from: 'MySite.com <[email protected]>',
        to: '[email protected]',
        subject: 'Email Test',
        text: 'Email Test' } };

    request(options, function (error, response, body) {
      if (error) throw new Error(error);

      console.log(body);
    });
});

I call this parse cloud code function from my website using the following coffee script:

   Parse.Cloud.run('sendemail', data, {
    success: () ->
      console.log(result)
      console.log("success")
    ,
    error: () ->
      console.log(error)
      console.log("fail")
  });

It seems like there is a setting on Back4App that I'm missing. I've searched all over the settings for Parse, and I don't see a place to specify the Access-Control-Allow-Origin. I've searched through API documentation for Back4App and the community group. The community group mentions the question here, but doesn't really give an answer other than contacting [email protected]. I've done that, but I need an answer soon.

There is also a test function on Back4App called hello that I can call fine and get a 200 response.

Parse.Cloud.define("hello", function(request, response) {
  response.success("Hello world!");
});

Upvotes: 4

Views: 1290

Answers (1)

Charles
Charles

Reputation: 576

I think you forgot { and } to define the function. You also have to use => instead ->.

Try to use "then" and "catch" from promise. Here are two snippets that you can use to test:

  Parse.Cloud.run('sendemail', data).then(() => {
      console.log(result)
      console.log("success")
    }).catch((error) => {
      console.log(error)
      console.log("fail")
    });

or this other one:

  Parse.Cloud.run('sendemail', data, {
    success: () => {
      console.log(result)
      console.log("success")
    },
    error: (error) => {
      console.log(error)
      console.log("fail")
    }
  });

Upvotes: 0

Related Questions