gpt4 book ai didi

javascript - 如何从 Firebase 数据快照中获取 key ?

转载 作者:行者123 更新时间:2023-12-03 01:54:38 24 4
gpt4 key购买 nike

我可以使用电子邮件地址查询我的 users 数组并返回用户的帐户信息:

users.orderByChild('email').equalTo(authData.user.email).once('value').then(function(snapshot) {
console.log(snapshot.val());
console.log(snapshot.key); // 'users'
console.log(snapshot.child('email').key); 'email'
...

enter image description here

如何获取 key (-KiBBDaj4fBDRmSS3j0r)。 snapshot.key 返回userssnapshot.child('email').key 返回 email。该 key 似乎不是子项,即它似乎位于 usersemail 之间。

最佳答案

你可以这样做:

var key = Object.keys(snapshot.val())[0];

引用号:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

关于javascript - 如何从 Firebase 数据快照中获取 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43615466/

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