Cloud functions for Firebase onWrite () always promise time

I try to run a cloud function and change the value in the database, but every time I return a promise with or without firebase-admin, the function expires after 60 seconds.

Here is my code:

var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.handleDisconnection = functions.database.ref('/pages/{pageId}/listeners/disconnection')
  .onWrite(event => {
    const eventSnapshot = event.data;
    const isDisconnected = eventSnapshot.val();
    const pageId = event.params.pageId;
    console.log('there is a new disconnection on page ' + pageId + ', isDisconnected: ' + isDisconnected);
    if (eventSnapshot.changed() && isDisconnected) {
      console.log('is disconnected true');
      return admin.database().ref('/pages/'+pageId+'/listeners/disconnection').set({ disconnection: false }).then(() => {
        console.log('Write succeeded!'); // this never triggers
      });
    }
});
+4
source share
2 answers
 if (eventSnapshot.changed() && isDisconnected) {
       console.log('is disconnected true');`
       return admin.database.ref('/...')
       .set({ disconnection: false }, err => {
                 if(!err)  // No error 
                       { console.log("Set Updated"); }
       });

set Methods Has a callback that is passed to err as an object; you can use err to get the status of operations.

0
source

Turns out the problem is not in the code.

Allowed updating Node.js and npm

Use the "Current" version https://nodejs.org/en/

-1

Source: https://habr.com/ru/post/1674230/


All Articles