【发布时间】:2020-07-10 01:34:06
【问题描述】:
我正在尝试读取谷歌云函数 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 });
【问题讨论】:
标签: javascript angular firebase google-cloud-firestore google-cloud-functions