Reputation: 848
I am trying to read the return value of a google cloud functions onCall function but I keep reading null.
This is my angular code:
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);
})
I have the following cloud function defined. Everything is bundled inside a transaction.
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
}
});
});
How can I return json objects as a promise? I have tried the following to no avail:
return Promise.resolve({ json object });
return Promise.reject({ json object });
Upvotes: 0
Views: 1004
Reputation: 317487
Your top-level function should return a promise that resolves with the data to send to the client. Returning that value from your transaction handler isn't enough. You'll need a return at the top level as well. Start with this:
return db.runTransaction(transaction => { ... })
As you can see from the API docs, runTransaction returns the promise returned by the transaction handler (or "updateFunction").
Upvotes: 2