【问题标题】:Issues Firebase cloud function call third party API using node JS使用节点 JS 发出 Firebase 云函数调用第三方 API
【发布时间】:2021-07-16 05:14:13
【问题描述】:

我尝试编写节点函数来调用第三方 API。我使用 Angular Fire 函数在 Angular 项目中显示结果。问题是没有数据响应;

这是我的节点 js 代码。

const request = require('request');
const UserDetail =  () => {

    const options ={
        url: 'https://www.reddit.com/r/funny.json',
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Accept-Charset': 'utf-8',
            'User-Agent': 'my-reddit-client'
        }
    }

    request(options, function(err, res, body) {
        let json = JSON.parse(body);
        console.log(json);
        
    });
     
}
UserDetail();

这是我的firebase函数代码:

exports.userdetails = functions.https.onRequest(require('./api/user/userdetail'));

这是我调用firebase函数代码的角度服务:

callUserDetails(){
         const details = this.functions.httpsCallable('userdetails')({ text: 'Some Request Data' })
         .pipe()
         .subscribe(resp => {
           console.log({ resp });
         }, err => {
           console.error({ err });
         });
          
  }

【问题讨论】:

  • 在您的UserDetail () 中,您是否从您的request 返回任何内容?
  • 是的,当我在节点 js 中运行时返回值没有问题,但是在我部署到 firebase 后,在角度运行时出现问题。

标签: node.js angular firebase google-cloud-functions


【解决方案1】:

也许你应该像这样调用 Angular 服务:

// provider class
constructor(private http: HttpClient) {}

this.http.get(url, {params: {}, headers: {}}).subscribe(result => {
  //// result 
})

【讨论】:

    【解决方案2】:

    您混淆了Callable Cloud FunctionsHTTPS Cloud Functions

    通过做

    exports.userdetails = functions.https.onRequest(...)
    

    您定义一个 HTTPS 云函数,

    但是通过做

    this.functions.httpsCallable('userdetails')({ text: 'Some Request Data' })
    

    在您的前端,您实际上调用了一个 Callable Cloud Function。


    您应该将您的云函数更改为可调用函数,或者通过向云函数 URL 发送 HTTP 请求来调用 userdetails HTTPS 云函数。

    我会推荐第一种方法,因为 Callable 比“简单”的 HTTPS 带来了几个优势(请参阅文档)。


    另外你需要注意request原生支持回调接口,但不返回Promise。并且需要使用 Promises 来管理 Callable Cloud Function 的生命周期(见官方video serie)。

    我会使用Axios 沿着以下几行(未经测试):

    exports.userdetails = functions.https.onCall(async (data, context) => {
    
        try {
    
            const options = {
                url: 'https://www.reddit.com/r/funny.json',
                method: 'get',
                headers: {
                    'Accept': 'application/json',
                    'Accept-Charset': 'utf-8',
                    'User-Agent': 'my-reddit-client'
                }
            }
    
            const axiosResponse = await axios(options);
    
            // Build the resp to be sent to the frontend by 
            // using axiosResponse.data .... up to you, see https://github.com/axios/axios#response-schema
    
            return { resp: .... }
    
        } catch (error) {
            // See https://firebase.google.com/docs/functions/callable#handle_errors
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 2017-12-26
      相关资源
      最近更新 更多