【问题标题】:Getting undefined in response when calling google cloud onCall Function调用谷歌云 onCall 函数时响应未定义
【发布时间】:2020-03-18 04:25:23
【问题描述】:

我写了2个类似的函数,一个是onRequest,一个是onCall(在documentation中提到)

函数/index.js

const products = [];
const LIMIT = 100;
for (let i = 0; i < LIMIT; i++) {
    products.push({
        name: "product" + i,
        price: "price" + i,
    });
}

exports.products1 = functions.https.onCall((input, context) => {
    const { page = 1, limit = 10 } = input;

    const startAt = (page - 1) * limit;
    const endAt = startAt + limit;

    return products.slice(startAt, endAt);
});

exports.products2 = functions.https.onRequest((request, response) => {
    const { page = 1, limit = 10 } = request.query;

    const startAt = (page - 1) * limit;
    const endAt = startAt + limit;

    return response.json(products.slice(startAt, endAt));
});

我可以使用 curl 从命令行调用 onRequest http 函数,但是当我尝试从应用程序代码调用 onCall 函数时(我在 react-native 上构建并使用 firebase 手机号码身份验证),我在回应。

App.js

async fetchProducts() {
        const instance = firebase.functions().httpsCallable('products1');
        try {
            const response = await instance({
                page: 1,
                limit: 15,
            })
                .then(res => console.warn('Res',res))
                .catch(err => console.warn('Error', err));
            console.warn('Response', response);
        } catch (httpsError) {
            console.log('Message', httpsError.message);

            // Check code
            if (httpsError.code === firebase.functions.HttpsErrorCode.NOT_FOUND) {
                console.error('Functions endpoint "order" not found');
            }
        }
    };

谁能告诉我我做错了什么?

【问题讨论】:

  • 您检查过可调用函数的协议文档吗?它们不能像普通的 HTTP 函数一样被对待。 firebase.google.com/docs/functions/callable-reference
  • 我读过。这是在文档中编写的 - 注意:在 JavaScript 客户端中,这些请求会触发 CORS OPTIONS 预检,因为:不允许 application/json。它必须是 text/plain 或 application/x-www-form-urlencoded。 Authorization 标头不是 CORS 安全列表中的请求标头。其他标题同样不允许。但是在 onCall 中,不需要显式传递授权标头还是存在?我也是 react-native 和 firebase 的新手。

标签: firebase firebase-authentication google-cloud-functions


【解决方案1】:

这是因为您同时使用了async/awaitthen() 方法。

如果你修改你的代码如下,它将起作用:

async fetchProducts() {
        const instance = firebase.functions().httpsCallable('products1');
        try {
            const response = await instance({
                page: 1,
                limit: 15,
            })
            console.warn('Response', response);
        } catch (httpsError) {
            console.log('Message', httpsError.message);

            // Check code
            if (httpsError.code === firebase.functions.HttpsErrorCode.NOT_FOUND) {
                console.error('Functions endpoint "order" not found');
            }
        }
    };

正如您将在MDN doc 中看到的那样,您根本不需要使用then()

await 表达式暂停异步函数的执行 (即httpsCallable('products1'))并等待通过的承诺 分辨率,然后恢复 async 函数的执行和 评估为解析值。

【讨论】:

  • @sumitsharma 嗨,您有时间查看建议的解决方案吗?
猜你喜欢
  • 2019-06-28
  • 2021-01-13
  • 2020-12-12
  • 2021-06-08
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
相关资源
最近更新 更多