Firebase-admin不会写入数据库,不会出现错误

时间:2021-11-29 20:27:41

Firebase admin isn't writing to the database.

Firebase admin没有写入数据库。

I am instantiating the database:

我正在实例化数据库:

var db = admin.database();

Then setting up a reference to the table I want:

然后建立对我想要的表的引用:

var systemsRef = db.ref("systems/");

I then have a function to check if the 'system', (an encrypted hardware id), exists.

然后我有一个函数来检查“系统”(加密的硬件id)是否存在。

function isSystemRegistered(id){
  var isTrue;
  systemsRef.once('value', function(snapshot) {
      isTrue = (snapshot.hasChild(id))? true : false;
  });
  return isTrue;
}

Which, as of yet returns false; which is true, because it doesn't exist yet. If the system doesn't exist, it writes the data.

它返回false;这是对的,因为它还不存在。如果系统不存在,它将写入数据。

const sysID = getSysID();
var sys.info.name = generateUniqueSystemName();

if(isSystemRegistered(sysID){
  console.log("system is already registered!");
} else {
  msystemsRef.set({
      sysID : sys.info.name
    }, function(error){
        console.log('There was an error while attempting to write to database: ' + error);
      });
  });
}

I've experimented, and temporarily made my prototype database fully public for a few minutes, just to be sure my rules weren't the issue... They weren't: still no bueno. No writes to the database... and no errors.

我做了实验,暂时将我的原型数据库完全公开了几分钟,只是为了确保我的规则不是问题所在……他们不是:仍然不是布埃诺。不对数据库进行写入……并没有错误。

I tried a different set, just be sure:

我尝试了不同的设置,只是确定:

msystemsRef.set("I'm writing data", function(error) {
  if (error) {
    alert("Data could not be saved." + error);
  } else {
    alert("Data saved successfully.");
  }
});

Again, I'm using an admin account, with public rules, so I should see a now I'm writing data table, just below root. Nothing...

同样,我使用的是管理帐户和公共规则,所以我现在应该看到我正在写一个数据表,就在root下面。没有什么……

So I switched tactics and attempted to push to the database with the canned tutorial, with my database still fully public:

所以我改变了策略,尝试用罐装的教程推送到数据库中,而我的数据库仍然是完全公开的:

systemsRef.push({sysID : sys.info.name});

And nothing... Want am I missing?

和什么……我想失踪?

2 个解决方案

#1


1  

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.

检查它们是否匹配——来自一个应用程序的凭据和来自另一个现有应用程序的databaseURL可以生成这样的结果。

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.

如果您没有从文件中加载凭据的数据,而是从其他地方加载,请确保它没有被修改——在将凭据的数据放入shell变量时,我在私钥中有一个问题。

#2


1  

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

在issystemregistration中,您将返回未定义的isTrue的同步值。

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

您应该返回.once('value')方法的承诺,并在调用方法附加一个then()以检查它是否存在。

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

您还可以使用snapshot.exist()检查存在的引用。

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?
  • 你运行什么NodeJS版本?
  • Did this run successfully on your PC?
  • 这个在你的电脑上运行成功吗?

#1


1  

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.

检查它们是否匹配——来自一个应用程序的凭据和来自另一个现有应用程序的databaseURL可以生成这样的结果。

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.

如果您没有从文件中加载凭据的数据,而是从其他地方加载,请确保它没有被修改——在将凭据的数据放入shell变量时,我在私钥中有一个问题。

#2


1  

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

在issystemregistration中,您将返回未定义的isTrue的同步值。

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

您应该返回.once('value')方法的承诺,并在调用方法附加一个then()以检查它是否存在。

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

您还可以使用snapshot.exist()检查存在的引用。

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?
  • 你运行什么NodeJS版本?
  • Did this run successfully on your PC?
  • 这个在你的电脑上运行成功吗?