Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – Firebase的云功能:如何向我的Cloud Endpoint发出请求大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在firebase数据库中写入某个值时向我的云端点项目发出请求.我找不到任何如何在Node.js中对端点执行请求的示例.这是我到目前为止所提出的:

"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);
       });
});

触发时,函数的日志喷口:TypeError:无法读取未定义的属性’init’.即使甚至不认识gapi.client.

首先,用于此请求的正确包装是什么? googleapis?请求承诺?

第二,我是否为端点调用设置了正确的路径和参数?假设端点函数是startCalc(int uid).

解决方法

更新

Cloud Functions for Firebase似乎阻止了对其App ENGIne服务的请求 – 至少在Spark计划中(即使它们都归谷歌所有 – 所以你假设“on the same network”).下面的请求适用于运行Node.js的本地计算机,但在函数服务器上失败,并出现getaddrinfo EAI_AGAIN错误,如here所述.显然,当您对运行的服务器执行请求时,它不是considered访问Google Api谷歌的App ENGIne.

无法解释为什么Firebase这里的倡导者会像火一样避开这个问题.

原始答案

想出来 – 切换到’request-promise’库:

"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);
    });
});

大佬总结

以上是大佬教程为你收集整理的node.js – Firebase的云功能:如何向我的Cloud Endpoint发出请求全部内容,希望文章能够帮你解决node.js – Firebase的云功能:如何向我的Cloud Endpoint发出请求所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。