我正在尝试搜索我的数据库以查看唯一 ID 是否已存在。数据库的布局如下:
我用来到达该选民
位置的代码是:
let ref = firebase.database().ref('players/' + playerId + '/voters');
ref.orderByChild(uid).equalTo(uid).once("value",snapshot => {
snapshot.forEach(child => {
ref.child(uid).set(1);
})
});
本例中的playerId是-L434GfPd9IXR8aOLfCR,其中-as uid是vKl6rlUul0WsbeWVORz3twPUfnd2
在某个时候,voters child 下会有很多 UID,但目前只有一个。我似乎无法找到正确的路径来搜索选民并检查它是否存在。如果它看到 voters
中 uid 中存储的唯一 ID,则会将该 uid 的值设置为 1。
我也不确定 snapshot.forEach 是否与此处相关
最佳答案
这应该是:
let ref = firebase.database().ref('players').child(playerId).child('voters');
ref.child(uid).once('value', snap => {
if (snap.exists) {
// a vote has already been recorded
} else {
// there is not already a vote there
}
});
关于javascript - 检查数据库中的现有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511372/