从Firebase Cloud Function调用Google Books API时出现HttpsError [英] HttpsError when calling Google Books API from Firebase Cloud Function

查看:71
本文介绍了从Firebase Cloud Function调用Google Books API时出现HttpsError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Firebase Cloud Function,该功能在Google Books API上执行搜索.

I'm trying to create a Firebase Cloud Function that performs a search on the Google Books API.

export const searchBooksOnline = functions.https.onCall(
    async function(data: any, context: functions.https.CallableContext) {
        const query: string = data.query;
        console.log(`Received query loud and clear: ${query}`);
        try {
            const res = await fetch(`https://www.googleapis.com/books/v1/volumes?q=${query}&key=MY-API-KEY`);
            const json = await res.json();
            return json;
        } catch(err) {
            throw new functions.https.HttpsError(err.status, 'Failed to search books online');
        }
    }
);

但是每当我从Javascript API调用此函数时,都会出现错误:

But whenever I call this function from the Javascript API, I get an error:

Unhandled error Error: Unknown error code: undefined.
    at new HttpsError (/srv/node_modules/firebase-functions/lib/providers/https.js:95:19)
    at exports.searchBooksOnline (/srv/lib/books.js:42:15)
    at func (/srv/node_modules/firebase-functions/lib/providers/https.js:267:32)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

这对我来说很神秘.我知道该函数正在正确调用,因为

which is quite cryptic to me. I know that the function is getting called properly because

  • 我可以在Cloud Functions日志大声且清晰地接收到查询:"中看到我的打印内容.

  • I can see my print in the Cloud Functions log "Received query loud and clear:".

当我删除 await fetch 行并返回一些伪数据时,该函数将正确执行.

When I remove the await fetch line and return some dummy data, the function executes correctly.

此外,我确定API调用是有效的,因为当我复制那几行以直接从浏览器的开发人员控制台运行时,我会得到预期的API响应.

Additionally, I'm sure that the API call is working because when I copy out those few lines to run directly from my browser's developer console, I get the expected API response.

我不确定导致此错误的Cloud Functions环境有何不同.(我正在执行Blaze计划,该计划应允许我向外部网站提出请求)

I'm not sure what is different about the Cloud Functions environment that is causing this error. (I'm on the Blaze plan, which should allow me to make requests to external sites)

希望有人可以对此有所帮助!

Hope that somebody can help shed some light on this!

(我见过类似的问题,但OP应用的解决方案对我没有影响,我仍然有相同的错误.)

(I have seen this similar issue, but the solution applied by the OP does not make a difference to me, I still have the same error.)

推荐答案

道格·史蒂文森(Doug Stevenson)的答案没有解决该问题直接发生,但帮助我进行了诊断:

The answer from Doug Stevenson didn't solve the problem directly, but helped me to diagnose it:

我按照他的建议修复了HttpsError报告后,立即尝试再次调用该函数,这一次它给了我一条错误消息,提示未定义'fetch'.结果我正在从客户端成功测试的 fetch 端JavaScript不是在CloudJS使用的NodeJS中本地实现的.

As soon as I fixed my HttpsError report as he suggested, I tried calling the function again and this time it gave me an error message saying 'fetch' not defined. Turns out that fetch which I was testing successfully from client-side JavaScript is not natively implemented in NodeJS (which Cloud Functions use).

因此,解决方法是仅对我的API请求使用其他方法.就我而言,我选择使用 node-fetch ,它实现了与JavaScript fetch 函数.

So the fix was simply to use a different method for my API request. In my case, I chose to use node-fetch, which implements the same interface as the JavaScript fetch function.

所以我要做的唯一改变就是做

So the only change I need to get things working was to do

npm install node-fetch @types/node-fetch # the @types package needed for TypeScript

然后

import fetch from 'node-fetch';

位于我的代码的顶部.

这篇关于从Firebase Cloud Function调用Google Books API时出现HttpsError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆