我正在尝试检查 Firebase 实时数据库中是否已存在某个名称,其结构如下:
这是我的代码:
const players = Firebase.database().ref("players");
function nameCheck() {
console.log(name);
players
.orderByChild('settlement_name')
.equalTo(name)
.once('value')
.then(snapshot => {
if(snapshot.exists()){
let playerData = snapshot.val();
console.log(playerData)
setError('Settlement name already exists')
return playerData
} else {
console.log('Settlement name not found');
}
})
}
谁能告诉我为什么这个函数不能正确识别名称存在的情况?我在函数开头记录名称,它与数据库中的名称相匹配,但它只会返回未找到该名称。
谢谢你。 🙂
最佳答案
您需要使用嵌套子路径,如 doc 中所述。 .
players
.orderByChild('settlement/settlement_name')
...
PS:如果您检查名称是否存在以便使用该名称创建新的数据库节点(如果不存在),则应该使用 Transaction .
关于javascript - 如何检查名称是否已存在于 Firebase 实时数据库(Firebase 和 JS)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69402526/