最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase get key of last node created - Stack Overflow

programmeradmin4浏览0评论

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 for limitToFirst() or limitToLast()? – 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
Add a ment  | 

1 Answer 1

Reset to default 7

You need to use limitToLast function.

this.cartFirebase.limitToLast(1).on('child_added', function(childSnapshot) {

     var snap = childSnapshot.val();

});
发布评论

评论列表(0)

  1. 暂无评论