Simple OnCall Return Null 我无法查明问题所在。
调用函数->
const schedule = () => {
const functions = getFunctions(getApp());
const scheduleAppointment = httpsCallable(functions, "scheduleOperation");
scheduleAppointment({ text: "messageText" })
.then((result) => {
console.log(result.data);
})
.catch((error) => {
console.error(error);
});
};
我所有的函数都存储在不同的文件夹中,例如“scheduleOperation”存储在“./appointment”中,我只是将它们从它们的文件夹导入到我的函数文件夹中的主要函数 Index.js。
在我开始填写我的功能之前,我只希望它返回简单的响应
Firebase 函数->
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const url = require("url");
const cors = require("cors")({ origin: true });
var serviceAccount = require("../serviceAccountKey.json");
exports.scheduleOperation = functions.https.onCall((data, context) => {
return{
data: {
firstNumber: "First",
};
});
//----------------------//
The Index function which calls Schedule operation->
//----------------------//
const { scheduleOperation } = require("./appointment");
const client = new Client({});
exports.scheduleOperation = functions.https.onCall((data, context) => {
scheduleOperation(data, context, client);
});
预期:
{
data: {
firstNumber: "First",
};
}
收到:
Null
任何想法?这仅适用于 onCall 函数 OnRequests 工作正常
Firebase 依赖项:“firebase”:“9.17.1”,
onCall
函数返回一个 Promise例如:
exports.scheduleOperation = functions.https.onCall((data, context) => {
return new Promise(resolve => {
resolve({ data: { firstNumber: "First" } });
});
});
让我知道你是怎么处理的。
所以答案是,你不能在 OnCall 和调用另一个 On Call,我试着这样做是为了让我的代码看起来更好,而不是在索引中破坏我的所有功能。
解决方法是在另一个文件夹中编写 OnCall 函数并像这样导入它 ->
OnCal Func 位于另一个文件夹->
exports.scheduleOperation = functions.https.onCall((request, response) => {
return new Promise((resolve) => {
resolve({ data: { firstNumber: "First" } });
});
});
然后使用 Require->
直接从索引中导入它const scheduleOperation = require("./appointment").scheduleOperation;
最后,直接从索引中导出 ->
exports.scheduleOperation = scheduleOperation;
希望这能帮助像我这样的新手