3

I am using firebase database and I want a cloud function(trigger) to communicate with a REST node.js webservice I have.

The trigger is already created, but now I want that the cloud function to call a external webservice I have in a hosted machine.

I imported jQuery. But it says that $.post is not a function. I thought it would be because the slim version was installed somehow(not the case). Then I tried to do it directly in JavaScript using XMLHttpRequest, which the function also says

ReferenceError: XMLHttpRequest is not defined at /user_code/index.js:91:19 at process._tickDomainCallback (internal/process/next_tick.js:129:7)

Do you have any clue how to make a POST request on firebase cloud functions?

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
João Mourão
  • 168
  • 1
  • 3
  • 14
  • I've been trying to do the same thing with same error. have also tried with npm packages: request, request-promise, superagent. Using these it always errors with getaddrinfo ENOTFOUND – Arkelyan May 11 '17 at 16:30
  • just found out getaddrinfo ENOTFOUND were happening because i was testing from a non-paid account. request & request-promise work if on paid account - on blaze plan – Arkelyan May 11 '17 at 17:34
  • Yup i was in the same situation, apparently the free version only allows outbound calls to google services. – João Mourão May 17 '17 at 10:34

2 Answers2

2

Firebase Cloud Functions is a Node.js environment. But jQuery is a client-side javascript library. There are a few server-side jQuery builds (Cheerio, nodeQuery), see here: Can I use jQuery with Node.js?

But I would . . .

try using node module xmlhttprequest. It let's you write raw requests the way you would in client-side JavaScript. You'll need that dependency in your package.json file ie:

"dependencies": {
    "xmlhttprequest": "^1.8.0"
}

Then in your function:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var request = new XMLHttpRequest();
request.addEventListener('load', doSomethingWithDataFromResponse);
request.open("GET","http://urlToMyServer");  
request.send();

function doSomethingWithDataFromResponse() {
  var data = this.responseText;
  //etc.
}

For a POST something like:

var request = new XMLHttpRequest();
var params = "word=foo"; //or stringify some JSON
request.addEventListener('load', doSomethingWithDataFromResponse);
request.open("POST","http://urlToMyServer"); 
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); // or application/json etc.
request.send(params);

That's how I've been doing requests from a node server to other third-party servers lately. I haven't had a chance to try it with Firebase yet, but all documentation seems to indicate that it should work.

Community
  • 1
  • 1
DMS
  • 133
  • 7
  • Even tho in the localhost i am ablle to use and import JQuery to node.js, inside google cloud functions i was not able to import them, it was always saying the package was not defined, request package was the one that worked properly with my solution – João Mourão May 17 '17 at 10:37
2

You have to be on a paid firebase plan (possibly Blaze) in order to POST to an external website. https://stackoverflow.com/a/42775841/6480950

Can use npm modules request & request promise.

Basic Test: https://stackoverflow.com/a/43645498/6480950

Community
  • 1
  • 1
Arkelyan
  • 250
  • 4
  • 14
  • Thank you very much :) this was it in the documentation could be a note because of that only after checking the billing price i understood :D thank you – João Mourão May 17 '17 at 10:33