Node.js 是否可以在Firebase云函数中的onCall函数结束之前返回数据
我想知道是否有可能在onCall函数完成之前返回数据。 以下是我正在尝试做的事情,我的应用程序中的结果数据始终等于null:Node.js 是否可以在Firebase云函数中的onCall函数结束之前返回数据,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我想知道是否有可能在onCall函数完成之前返回数据。 以下是我正在尝试做的事情,我的应用程序中的结果数据始终等于null: exports.myFunction = functions.https.onCall((data, context) => { if (!context.auth || !context.auth.uid) { return { statut: "NOK", }; } return Promise.all([promise
exports.myFunction = functions.https.onCall((data, context) => {
if (!context.auth || !context.auth.uid) {
return {
statut: "NOK",
};
}
return Promise.all([promise1(), promise2()])
.then(results => {
const result1 = results[0].data();
return FIRESTORE.collection('MyCollection')
.add(result1)
.then(results2 => {
let queries = [];
queries.push(
function1234()
);
queries.push(
FIRESTORE.collection('MyCollection2')
.doc("123")
.set({a: 123})
);
return Promise.all(queries)
.then(r => {
return { myReturn: "AAAA" };
});
})
.then(r2 => {
console.log('0000');
return function9898();
})
})
这是我的客户端功能:
myFunction = () => {
var myFunctionCloud = firebase.functions().httpsCallable('myFunction');
myFunctionCloud().then(function (result) {
console.log(JSON.stringify(result));
})
}
我的日志:
{“数据”:null}
我想知道是否有可能在我的onCall之前返回数据
功能完成
不,这是不可能的,因为在可调用的云函数中返回数据向云函数平台表明,可调用的云函数已完成,并且它可以清除该调用中的所有内容。因此,在许多情况下,您的云功能将在业务逻辑完全执行之前终止
在某些情况下,云功能平台可能不会立即终止您的CF,给您足够的时间让您的业务逻辑完全执行,但这根本不能保证。不建议依赖此特定案例,这实际上会导致一些“不稳定”的行为
将结果立即发送回前端并继续在后台工作的方法之一是调用a(消息中包含所有相关输入),然后返回值。换句话说,您将剩余的工作委托给另一个云函数,并通过返回响应停止当前可调用的函数。@Joris Hi,您是否有时间查看建议的答案?