Firebase-admin不会写入数据库,不会出现任何错误 [英] Firebase-admin won't write to database, gives no errors
问题描述
Firebase管理员未写入数据库.
Firebase admin isn't writing to the database.
我正在实例化数据库:
var db = admin.database();
然后设置对我想要的表的引用:
Then setting up a reference to the table I want:
var systemsRef = db.ref("systems/");
然后我有一个功能来检查系统"(加密的硬件ID)是否存在.
I then have a function to check if the 'system', (an encrypted hardware id), exists.
function isSystemRegistered(id){
var isTrue;
systemsRef.once('value', function(snapshot) {
isTrue = (snapshot.hasChild(id))? true : false;
});
return isTrue;
}
截至目前还返回false
;这是true
,因为它尚不存在.如果系统不存在,它将写入数据.
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.
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);
});
});
}
我已经进行了试验,并暂时将我的原型数据库完全公开了几分钟,以确保我的rules
不是问题……他们不是:仍然不是布宜诺斯艾利斯.没有写入数据库...,并且没有错误.
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.
我尝试了不同的set
,只需确保:
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.");
}
});
同样,我使用的是具有public
规则的管理员帐户,因此我应该在根目录下看到一个现在的I'm writing data
表.没事...
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...
因此,我改变了策略,并尝试使用罐装教程将数据库推送到数据库,并且我的数据库 still 完全公开:
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?
推荐答案
在isSystemRegistered
中,您将返回isTrue
的同步值,即undefined
.
In isSystemRegistered
you're returning the synchronized value of isTrue
which is undefined
.
您应该返回.once('value')
方法的承诺,并在调用方法中附加then()
以检查它是否存在.
You should return the promise of the .once('value')
method and in the calling method attach a then()
to check if it exists.
您还可以使用snapshot.exists()
来检查引用是否存在.
You can also use the snapshot.exists()
to check on a reference for existence.
建议的修改:
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);
});
}
在Raspberry Pi上运行会引发更多问题.
Running on Raspberry Pi raises some more questions.
- 它如何连接到互联网?
- 连接速度有多快?
- 您运行的是哪个NodeJS版本?
- 此操作是否已在您的PC上成功运行?
这篇关于Firebase-admin不会写入数据库,不会出现任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!