[英]cloud functions onCall - how to return an object
我正在嘗試讀取谷歌雲函數 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 對象作為承諾返回? 我嘗試了以下方法無濟於事:
return Promise.resolve({ json object });
return Promise.reject({ json object });
您的頂級函數應該返回一個承諾,該承諾與要發送給客戶端的數據一起解析。 從您的事務處理程序返回該值是不夠的。 您還需要在頂級回報。 從這個開始:
return db.runTransaction(transaction => { ... })
正如您從 API 文檔中看到的, runTransaction返回事務處理程序(或“updateFunction”)返回的承諾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.