firebase获取最后一个节点的密钥 [英] firebase get key of last node created
本文介绍了firebase获取最后一个节点的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取我刚刚在firebase上推送的最后一个节点的密钥,但我的代码仍然出错
I am trying to get the key of the last node I have just pushed to on firebase but my code keeps erroring
constructor (props) {
super(props)
this.cartFirebase = firebaseApp.database().ref('carts')
}
componentDidMount () {
this.cartFirebase.child(DeviceInfo.getUniqueID() + '/items').push({
item: this.props.data.name,
qty: 1
})
this.cartFirebase.limit(1).on('child_added', function(snapshot) {
console.log(snapshot)
})
}
推荐答案
您需要使用 limitToLast
函数。
this.cartFirebase.limitToLast(1).on('child_added', function(childSnapshot) {
var snap = childSnapshot.val();
});
这篇关于firebase获取最后一个节点的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文