How to write a firebase transaction in angular6/typescript

Hello,

I am trying to figure out how to write transactions to a firebase database from an angular 6 / typescript application.

One working example I am using as a guide is the following:

const transactions = [];

return db.runTransaction(function(transaction) {

            // const promises = [];

            descriptionsInDB.forEach(dbDescription => {

                if (dbDescription.matched === false) {

                    // item wasn’t found, so add the item to the transaction.

                    const phraseItem: PhrasesDB = {

                        query: dbDescription.description,

                        updatedAt: Date.now(),

                        createdAt: Date.now(),

                        workflows: {

                            [workflowId]: true

                        }

                    };

                    const newPhrasesRef = phrasesCol.doc();

                    transactions.push(transaction.set(newPhrasesRef, phraseItem));

                } else {

                    if (dbDescription.data.workflows && dbDescription.data.workflows[workflowId]) {

                        // do nothing, this phrase is already part of the record.

                    } else { // update the workflows that are part of the record.

                        const workflows = {

                            workflows: {

                                [workflowId]: true

                            }

                         };

                        const phrasesRef = phrasesCol.doc(dbDescription.dbId);

                        transactions.push(transaction.update(phrasesRef, workflows));

                    }

                }

            });

            return Promise.all(transactions);

        })

The key pieces I’ve identified are as follows:

  1. Create an array to hold your transactions: const transactions = [];
  2. Begin the transaction: return db.runTransactions(function(transaction) {…
  3. Execute a DB query using the transaction: transaction.set(newPhraseRef, phraseItem);
  4. Push the transaction returned from the query to the transaction array: transactions.push(transaction.set(newPhraseRef, phraseItem));
  5. Return a promise with the transaction array: return Promise.all(transaction);

If I have this right, then I should be able to apply this formula to the transaction I’m trying to write, and it should work:

        const pendingRef = Pending/${req.query.inviteId};

        const acceptance = {

          ‘cryptoInvitationAcceptance’: req.body.cryptoInvitationAcceptance,

          ‘reason’: (req.body.reason !== undefined ? req.body.reason : ‘’)

        }

return db.runTransaction(function(t) {

          const transArray = [];

          const docRef = db.collection(‘Pending’).doc(req.query.inviteId);

          transArray.push(t.set(docRef, acceptance));

          return Promise.all(transArray);

        }).then(result => {

          console.log('result = ', result);

        }).catch(err => {

          console.log('err = ', err);

        });

But it always goes to the catch block and prints out the message:

err =  Error: You must return a Promise in your transaction()-callback.

But I AM returning a promise, aren’t I? The line: return Promise.all(transArray) is the promise I’m returning. No?