gpt4 book ai didi

javascript - 当 Firebase 中父键未知时检索子记录

转载 作者:行者123 更新时间:2023-11-27 23:38:04 25 4
gpt4 key购买 nike

我有这样的数据:

"customers": {
"aHh4OTQ2NTlAa2xvYXAuY29t": {
"customerId": "xxx",
"name": "yyy",
"subscription": "zzz"
}
}

我需要通过 customerId 检索客户。由于路径限制,父 key 只是 B64 编码的邮件地址。通常我通过这个电子邮件地址查询数据,但在少数情况下我只知道 customerId。我试过这个:

getCustomersRef()
.orderByChild('customerId')
.equalTo(customerId)
.limitToFirst(1)
.once('child_added', cb);

如果客户确实存在,这会很好地发挥作用。在相反的情况下,永远不会调用回调。

我尝试了 value 事件,该事件有效,但这给了我以编码的电子邮件地址开头的整个树,因此我无法访问内部的实际数据。或者我可以吗?

我找到了这个答案Test if a data exist in Firebase ,但这再次假设您知道所有路径元素。

getCustomersRef().once('value', (snapshot) => {
snapshot.hasChild(`customerId/${customerId}`);
});

我在这里还能做什么?

更新

我想我找到了解决方案,但感觉不对。

let found = null;
snapshot.forEach((childSnapshot) => {
found = childSnapshot.val();
});
return found;

最佳答案

旧;误解了问题:

If you know the "endcodedB64Email", this is the way.:

var endcodedB64Email = B64_encoded_mail_address;

firebase.database().ref(`customers/${endcodedB64Email}`).once("value").then(snapshot => {
// this is getting your customerId/uid. Remember to set your rules up for your database for security! Check out tutorials on YouTube/Firebase's channel.
var uid = snapshot.val().customerId;
console.log(uid) // would return 'xxx' from looking at your database

// you want to check with '.hasChild()'? If you type in e.g. 'snapshot.hasChild(`customerId`)' then this would return true, because 'customerId' exists in your database if I am not wrong ...
});

UPDATE (correction) :

We have to know at least one key. So if you under some circumstances only know the customer-uid-key, then I would do it like this.:

// this is the customer-uid-key that is know. 
var uid = firebase.auth().currentUser.uid; // this fetches the user-id, referring to the current user logged in with the firebase-login-function
// this is the "B64EmailKey" that we will find if there is a match in the firebase-database
var B64EmailUserKey = undefined;

// "take a picture" of alle the values under the key "customers" in the Firebase database-JSON-object
firebase.database().ref("customers").once("value").then(snapshot => {
// this counter-variable is used to know when the last key in the "customers"-object is passed
var i = 0;
// run a loop on all values under "customers". "B64EmailKey" is a parameter. This parameter stores data; in this case the value for the current "snapshot"-value getting caught
snapshot.forEach(B64EmailKey => {
// increase the counter by 1 every time a new key is run
i++;
// this variable defines the value (an object in this case)
var B64EmailKey_value = B64EmailKey.val();
// if there is a match for "customerId" under any of the "B64EmailKey"-keys, then we have found the corresponding correct email linked to that uid
if (B64EmailKey_value.customerId === uid) {
// save the "B64EmailKey"-value/key and quit the function
B64EmailUserKey = B64EmailKey_value.customerId;

return B64UserKeyAction(B64EmailUserKey)
}

// if no linked "B64EmailUserKey" was found to the "uid"
if (i === Object.keys(snapshot).length) {
// the last key (B64EmailKey) under "customers" was returned. e.g. no "B64EmailUserKey" linkage to the "uid" was found
return console.log("Could not find an email linked to your account.")
}
});
});

// run your corresponding actions here
function B64UserKeyAction (emailEncrypted) {
return console.log(`The email-key for user: ${auth.currentUser.uid} is ${emailEncrypted}`)
}

I recommend putting this in a function or class, so you can easily call it up and reuse the code in an organized way.

我还想补充一点,必须定义 Firebase 规则以确保一切安全。如果必须计算敏感数据(例如价格),请在 Firebase 的服务器端执行此操作!使用云函数。这是 Firebase 2017 的新增功能。

关于javascript - 当 Firebase 中父键未知时检索子记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33955515/

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