gpt4 book ai didi

javascript - 如何检查数据是否存在?

转载 作者:行者123 更新时间:2023-12-03 02:13:41 25 4
gpt4 key购买 nike

Firebase 规则配置:

{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}

Firebase 数据库数据示例

enter image description here

使用这个idea ,我想出了如下的东西,但没有工作。

const { currentUser } = firebase.auth();

console.log(currentUser.uid);

firebase.database().
ref(`/users/${currentUser.uid}/userSettings`)
.child({ userSetting_DisplayName, userSetting_City, userSetting_DailyLimit })
.once('value', function(snapshot) {
if (snapshot.exists()) {
console.log('exist');
}else{
console.log('not exist');
}
});

哪个部分可能出了问题?

最佳答案

将其更改为:

firebase.database().
ref(`/users/${currentUser.uid}/userSettings`)
.once('value', function(snapshot) {
if (snapshot.exists()) {
console.log('exist');
}else{
console.log('not exist');
}
});

然后您将能够检查userSettings下是否存在数据

或者如果您想检查特定子项是否具有特定数据,那么您可以这样做:

ref(`/users/${currentUser.uid}/userSettings`).orderByChild("userSetting_city").equalTo(valuehere).once(...

或者如果您想检查是否有特定的 child :

ref(`/users/${currentUser.uid}/userSettings`).child("userSetting_DisplayName").once(...

同样来自文档:

child

child(path) returns firebase.database.Reference

Gets a Reference for the location at the specified relative path.

The relative path can either be a simple child name (for example, "ada") or a deeper slash-separated path (for example, "ada/name/first").

https://firebase.google.com/docs/reference/js/firebase.database.Reference#child

此外, child 被用来走过一条路径,所有这些 child 都在同一层,所以它不起作用。

关于javascript - 如何检查数据是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49445120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com