How to exit firebase-admin nodejs script correctly when all transactions are completed

In any case, to check if the whole firebase transaction in the firebase-admin nodejs script has completed, and properly disconnect from firebase and exit the nodejs script?

Currently, the firebase-admin node script will continue to work even after the completion of the entire transaction.

+4
source share
2 answers

The method DatabaseReference.transaction()returns a promise that is executed when the transaction is completed. You can use Promise.all()to wait for the completion of any number of these promises transactions, and then call process.exit()to complete the program.

Here is a complete example:

var admin = require("firebase-admin");

// Initialize the SDK
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

// Create two transactions
var addRef = admin.database().ref("add");
var addPromise = addRef.transaction(function(current) {
  return (current || 0) + 1;
});

var subtractRef = admin.database().ref("subtract");
var subtractPromise = subtractRef.transaction(function(current) {
  return (current || 0) - 1;
});

// Wait for all transactions to complete and then exit
return Promise.all([addPromise, subtractPromise])
  .then(function() {
    console.log("Transaction promises completed! Exiting...");
    process.exit(0);
  })
  .catch(function(error) {
    console.log("Transactions failed:", error);
    process.exit(1);
  });
+3
source

promises, , , , , :

app.delete()

, , :

firebase.app().delete()
+1

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


All Articles