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')
}
ponentDidMount () {
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)
})
}
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')
}
ponentDidMount () {
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)
})
}
Share
Improve this question
edited May 22, 2017 at 8:30
Boss Nass
asked May 21, 2017 at 14:25
Boss NassBoss Nass
3,5229 gold badges49 silver badges93 bronze badges
4
- What's the error you get? – Frank van Puffelen Commented May 21, 2017 at 15:01
- hi @FrankvanPuffelen added the error – Boss Nass Commented May 22, 2017 at 8:30
-
As the error message says
limit()
is not a function. Are you looking forlimitToFirst()
orlimitToLast()
? – Frank van Puffelen Commented May 22, 2017 at 14:20 - limit to last, wanted to get the most recent node that has been created – Boss Nass Commented May 23, 2017 at 9:01
1 Answer
Reset to default 7You need to use limitToLast
function.
this.cartFirebase.limitToLast(1).on('child_added', function(childSnapshot) {
var snap = childSnapshot.val();
});