Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 进行计算后更新firebase数据_Javascript_Angular_Firebase_Firebase Realtime Database_Ionic2 - Fatal编程技术网

Javascript 进行计算后更新firebase数据

Javascript 进行计算后更新firebase数据,javascript,angular,firebase,firebase-realtime-database,ionic2,Javascript,Angular,Firebase,Firebase Realtime Database,Ionic2,我正在使用Firebase作为我的Ionic 2应用程序的数据库 我编写了以下方法将所有子节点中的所有年龄字段更新为7(用于测试目的): 上述代码运行良好 我现在需要做一个计算,然后更新,而不是更新年龄值 我需要使用以下方法从Firebase获取duedate,并根据今天计算持续时间: setduration(lastday){ var today= moment().format('L'); var durationd = moment(lastday).diff(mome

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

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

上述代码运行良好

我现在需要做一个计算,然后更新,而不是更新年龄值

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

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

但是,当我调用该方法[就像调用setAgemethod()]一样]时,它无法得到结果。如何更改代码以解决此问题?

我相信您希望通过考虑数据库中已经可用的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);
    }
multiupdate(){
var-pc;
this.memberProfileref.child('/members/')。once('value',(快照)=>{
snapshot.forEach(子项=>{
this.setAge(child.key);//将所有年龄字段更新为7(工作正常)

this.setduration(child.val().duedate);//我相信您希望通过考虑数据库中已经可用的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);
    }
multiupdate(){
var-pc;
this.memberProfileref.child('/members/')。once('value',(快照)=>{
snapshot.forEach(子项=>{
this.setAge(child.key);//将所有年龄字段更新为7(工作正常)

this.setduration(child.val().duedate);//你的问题不清楚。你把什么传递给setduration?它是
duedate
?你在
conlose.log(dd)
的输出中看到了什么?另外,我会使用
moment().valueOf()
比较两个时间戳。@Ari 7感谢您的回复。是的,我需要逐个遍历所有chidnodes并获取duedate,然后将其传递给方法setduration()然后,在firebase中更新字段duration.in。你的问题不清楚。你把什么传递给setduration?它是
duedate
?你在
conlose.log(dd)
的输出中看到了什么?另外,我会使用
moment().valueOf()
比较两个时间戳。@Ari 7感谢您的回复。是的,我需要逐个遍历所有chidnodes并获取duedate,然后将其传递给方法setduration()获取持续时间。之后,在firebase中更新字段持续时间。这正是我需要的。现在工作正常。谢谢。这正是我需要的。现在工作正常。谢谢。