Cloud Features for Firebase: How to Send a Request to My Cloud Endpoint

I am trying to send a request to a cloud endpoint project when a specific value is written to the firebase database. I cannot find any example on how to make a request to endpoints in Node.js. This is what I have come up with so far:

"use strict";
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const gapi = require('googleapis');

admin.initializeApp(functions.config().firebase);

exports.doCalc = functions.database.ref('/users/{uid}/calc').onWrite(event => {
    return gapi.client.init({
            'apiKey': 'AIzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'clientId': '1234567890-xxx.apps.googleusercontent.com',
            'scope': 'donno what to put here'
       }).then(function() {
           return gapi.client.request({
               'path': 'https://myproj.appspot.com/_ah/api/myApi/v1',
               'params': {'query': 'startCalc', uid: event.params.uid }
           })
       }).then(function(response) {
           console.log(response.result);
       }, function(reason) {
           console.log('Error: ' + reason.result.error.message);
       });
});

      

When launching trap functions: TypeError: Cannot read property 'init' of undefined

. that is, it doesn't even recognize gapi.client.

First, what is the correct package to use for this request? googleapis? Promise Request?

Second, am I setting the correct path and parameters to call the endpoint? Suppose the function is an end point startCalc(int uid)

.

+3


source to share


1 answer


Update

It looks like the Cloud features for Firebase are blocking requests to the App Engine service - at least according to Spark's plan (although they are both owned by Google, so you can assume on the same network. ") The following query runs on a local machine running Node.js but the function server fails with an error getaddrinfo EAI_AGAIN

as described here Obviously this does not count as accessing the Google API when making a request to your server running on Google App Engine.

Can't explain why the Firebase advocates here are avoiding this question like fire.



Original Answer

It turned out - switched to the "request-promise" library:

"use strict";
const functions = require('firebase-functions');
const request = require('request-promise');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.doCalc = functions.database.ref('/users/{uid}/calc').onWrite(event => {
    return request({
        url: `https://myproj.appspot.com/_ah/api/myApi/v1/startCalc/${event.params.uid}`,
        method: 'POST'
    }).then(function(resp) {
        console.log(resp);
    }).catch(function(error) {
        console.log(error.message);
    });
});

      

+5


source







All Articles