在进行计算后更新firebase数据

时间:2017-07-05 19:43:24

标签: javascript angular firebase firebase-realtime-database ionic2

我正在使用Firebase作为我的Ionic 2应用程序的数据库。

enter image description here

我编写了以下方法来将所有子节点中的所有年龄字段更新为值7(用于测试目的):

def search
    @users = User.all
    @biens = Bien.near(params[:location], 3, units: :km).where(room_type: params[:room_type], nb_piece: params[:nb_piece])
    @hash = Gmaps4rails.build_markers(@bien) do |bien, marker|
      marker.lat @bien.latitude
      marker.lng @bien.longitude
    end
  end

以上代码运作良好。

考虑到更新年龄值,我现在需要进行计算,然后进行更新。

我需要从Firebase获取 duedate ,使用以下方法并根据今天计算持续时间:

  multiupdate(){
  var pc;

  this.memberProfileref.child('/members/').once('value',(snapshot)=> {

      snapshot.forEach(child=> {

          this.setAge(child.key);     // Will update all the age fields to 7 (works well)

          return false;
      });   
   });
}

setAge(UserId){
    this.memberProfile.child(UserId).update({
       age:7
    });
}

但是当我调用方法时[正如我对setAgemethod()所做的那样),它无法获得结果。如何更改代码来解决这个问题?

1 个答案:

答案 0 :(得分:0)

我相信你想通过考虑数据库中已有的duedate来发出每个节点的持续时间。检查下面的代码,看看我如何调用你的方法。

 multiupdate(){
      var pc;

      this.memberProfileref.child('/members/').once('value',(snapshot)=> {

          snapshot.forEach(child=> {

              this.setAge(child.key); // Will update all the age fields to 7 (works well)
              this.setduration(child.val().duedate); //<-- call your method like this

              return false;
          });   
       });
    }

    setAge(UserId){
        this.memberProfile.child(UserId).update({
           age:7
        });
    }

    setduration(lastday){
       var today= moment().format('L');
          var durationd = moment(lastday).diff(moment(today));
          var dd=moment.duration(durationd).as('days');
         console.log(dd);
    }