云函数onCall-如何返回对象



我试图读取谷歌云函数onCall函数的返回值,但我一直读取null。

这是我的角度代码:

const callable = this.angularFireFunctions.httpsCallable('createEvent');
callable(this.formGroup.value).toPromise()
.then((next) => {
console.log(next); //function succeeds but returned null
})
.catch((err) => {
console.log(err);
})

我定义了以下云函数。所有东西都捆绑在一个交易中。

export const createEvent = functions.https.onCall((data: any, context: CallableContext) : any | Promise<any> =>
{
const user_DocumentReference = db.collection('users').doc(context.auth?.uid);
const event_DocumentReference = db.collection('events').doc();
db.runTransaction((transaction: FirebaseFirestore.Transaction) =>
{
return transaction.getAll(user_DocumentReference)
.then((documentSnapshots: FirebaseFirestore.DocumentSnapshot<any>[]) =>
{
const user_DocumentSnapshot = documentSnapshots[0].data();
if (typeof user_DocumentSnapshot === 'undefined')
{
// return some error message json object
}
transaction.create(event_DocumentReference,
{
//json object
});
//return success object with few attributes
})
.catch((firebaseError: FirebaseError) =>
{
//return some error message json object
}
});
});

如何将json对象作为promise返回?我尝试了以下操作,但没有成功:

return Promise.resolve({ json object });
return Promise.reject({ json object });

您的顶级函数应该返回一个promise,该promise使用要发送到客户端的数据进行解析。从事务处理程序返回该值是不够的。你也需要一个顶级的回报。从这个开始:

return db.runTransaction(transaction => { ... })

从API文档中可以看到,runTransaction返回事务处理程序(或"updateFunction"(返回的promise。

最新更新