Auto increment a value in firebase with javascript

后端 未结 5 2097
野性不改
野性不改 2020-12-03 14:52

Hello I\'m working on some firebase project and i can save my datas via javascript into firebase database. But i couldn\'t figure it out to auto increment child value (my ch

5条回答
  •  遥遥无期
    2020-12-03 15:23

    IMHO, I think we should use transaction to save increment_id. Please take a look here: https://firebase.google.com/docs/database/web/read-and-write#save_data_as_transactions

    function toggleStar(postRef, uid) {
      postRef.transaction(function(post) {
        if (post) {
          if (post.stars && post.stars[uid]) {
            post.starCount--;
            post.stars[uid] = null;
          } else {
            post.starCount++;
            if (!post.stars) {
              post.stars = {};
            }
            post.stars[uid] = true;
          }
        }
        return post;
      });
    }
    

提交回复
热议问题