【发布时间】: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