如何获得firebase id [英] How to get firebase id
本文介绍了如何获得firebase id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都知道如何获得Firebase唯一ID?我试过 name()
, name
, key
, key()
。什么都行不通。
Anyone know how to get the Firebase unique id? I've tried name()
, name
, key
, key()
. Nothing works.
我能够看到数据,但我不知道如何获取ID。我需要它。
I am able to see the data but I have no idea how to get the id back. I need it.
//Create new customers into firebase
function saveCustomer(email) {
firebase.database().ref('/customers').push({
email: email
});
firebase.database().ref('/customers').on("value", function(snapshot) {
console.log(snapshot.val());
console.log(snapshot.value.name());
}, function(errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
推荐答案
致电 推送
将返回Firebase参考。如果您使用的是Firebase 3 API,则可以从参考文献 key
property:
The call to push
will return a Firebase reference. If you are using the Firebase 3 API, you can obtain the unique key of the pushed data from the reference's key
property:
var pushedRef = firebase.database().ref('/customers').push({ email: email });
console.log(pushedRef.key);
推送数据的密钥是在客户端上生成的 - 使用时间戳和随机数据 - 并且立即可用。
The key for the pushed data is generated on the client - using a timestamp and random data - and is available immediately.
这篇关于如何获得firebase id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文