Firebase-admin won't write to database, gives no errors

落花浮王杯 提交于 2019-12-04 16:57:06

Make sure the credentials and databaseURL are correct.

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: databaseURL
});

Check if they're matching - credential from one app and databaseURL from another existing app could produce such result.

If you are not loading credential's data from file but from somewhere else, make sure it's not modified - I had an issue with newlines in private key when putting the credential's data to shell variable.

In isSystemRegistered you're returning the synchronized value of isTrue which is undefined.

You should return the promise of the .once('value') method and in the calling method attach a then() to check if it exists.

You can also use the snapshot.exists() to check on a reference for existence.

Edit:

Suggested edit:

var systemRef = admin.database('system');

function isSystemRegistered(id) {
  return systemRef.child(id).once('value')
    .then(function (snap) {
      return snap.exists();
    });
}

function writeData(aSystem) {
  var sysId = generateUniqueSystemName();
  return systemRef.child(sysId)
    .once('value')
    .then(function (snapshot) {
      if (!snapshot.exists()) {
        return systemRef.child(sysId).update(aSystem);
      }

      // Here `sysId === snapshot.key`
      return systemRef.child(sysId).set(aSystem);
    })
    .catch(function (err) {
       console.error(err);
    });
}

Running on Raspberry Pi raises some more questions.

  • How does it connect to the internet?
  • How fast is the connection?
  • What NodeJS version do you run?
  • Did this run successfully on your PC?

Same issue here. I eventually realised that the database went offline before the command was even sent to firebase. This made sense because there was no error, since it never even sent the request.

Eg. .set({blah: 123}) does not immediately transmit to the server. Instead, something is placed on the node event queue to execute. If you let the database go offline too soon, it won't process the queue.

Perhaps (like me) you're calling admin.database().goOffline(); at the end of the script? If so, you may just need to defer or delay the offline method until after the transmission.

// (TYPESCRIPT)

import * as admin from 'firebase-admin';

let app = admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    databaseURL: "https://blahblah.firebaseio.com/", 
});

admin.database().ref().push({ something: 123 }).then(() => {
    console.log("push complete");

});

// delay before going offline
setTimeout(() => {
    admin.database().goOffline();
    process.abort();
}, 2000);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!