earlyriser
earlyriser

Reputation: 439

Why my Firestore transaction called via Firebase Functions is timing out?

In the function logs I can see it's running and the updates are applied to the documents, however it's timing out.

The logs show:

11:41:47.474 PM addContact Function execution took 60003 ms, finished with status: 'timeout'

11:40:47.743 PM addContact Transaction success!

11:40:47.659 PM addContact 9 9

11:40:47.659 PM addContact

11:40:47.659 PM addContact 8 8

11:40:47.656 PM addContact

11:40:47.472 PM addContact Function execution started

exports.addContact = functions.https.onRequest((req, res) => {

let user_id="fhr..."
let asker_id="wnz...";
let giver_id="Sp7...";

//const requestRef = admin.firestore().collection('contactRequests').doc(user_id)
const askerRef = admin.firestore().collection('users').doc(asker_id)
const giverRef = admin.firestore().collection('users').doc(giver_id)


var transaction = admin.firestore().runTransaction(t => {
  return t.getAll(giverRef, askerRef)
    .then(docs => {
      const id1 = docs[0];
      const id2 = docs[1];

      if ((id1.exists && id2.exists)) {
        // do stuff

        let giverContacts = docs[0].data().foo
        let askerContacts = docs[1].data().foo

        console.log("****")
        console.log(giverContacts, askerContacts)

        giverContacts=giverContacts+1
        askerContacts=askerContacts+1

        console.log("****")
        console.log(giverContacts, askerContacts)

        t.update(giverRef, {foo: giverContacts})
        t.update(askerRef, {foo: askerContacts})

        return true
      } else {
        throw new Error();
      }

    })
})
.then(result => console.log('Transaction success!') )
.catch(err => console.log('Transaction failure:', err) );

return transaction;

});

Upvotes: 0

Views: 342

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317362

It's timing out because you never send a response to the client. HTTP type functions terminate only after a response is sent. You can send anything at all:

.then(result => {
    console.log('Transaction success!')
    res.send("OK")  // send anything at all to terminate the function
})

For all other types of functions, you have to return a promise the resolves when all the work is complete.

Please read the documentation for more information.

Upvotes: 2

Related Questions