且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Firebase Cloud Function调用Google Books API时出现HttpsError

更新时间:2023-01-18 13:25:07

道格·史蒂文森(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';

位于我的代码的顶部.