forEach方法返回所有数据

时间:2016-03-20 22:39:09

标签: javascript firebase rxjs

firebase在学习过程中我遇到了一个问题或者forEach方法的误解,我可以返回forEach方法的值,但是我只有几个数据的数据,怎样才能返回这个迭代来拥有我的所有数据this.momo对象中可用的数据。

laodMessages(UserId){
  return Observable.create((observer) => {
    this.rootRef.child('users').child(UserId).child('rooms').on('child_added', snapshot => {
      this.groupKey = snapshot.key();
      this.rootRef.child('messages').orderByKey().equalTo(this.groupKey).once('child_added', (snapshot) => {
        this.key = snapshot.key();
        this.momo = {key: this.key, 'want to have the value of the forEach method here'}
        snapshot.forEach((data) => {
          this.haha = data.val();
          return true 
        })
        observer.next(this.momo)
      })
    })
  })
}

数据结构:

{
  "messages" : {
    "-KD8ky8mUR2US3eFBxVR" : {
      "-KDI0FWCEBpsfX-Z92Lm" : {
        "messages" : "hahaha",
        "sender" : "facebook:1016794731699551",
        "time" : "time"
      },
    },
    "-KD972O2fD9gWPDtxra8" : {
      "-KDI0_YDbneuSJN1_N3i" : {
        "messages" : "hahaha",
        "sender" : "facebook:1016794731699551",
        "time" : "time"
      },
      "-KDKdL1gXeBaK1rthsiP" : {
        "messages" : "moi aussi",
        "sender" : "facebook:1016794731699551",
        "time" : "time"
      }
    },
    "-KD9BQFnmX8qIgL8hwRE" : {
      "-KDKaiZEev4eqnlOWIrU" : {
        "message" : "this.message",
        "sender" : "facebook:1016794731699551",
        "time" : "time"
      }
    }
  },
  "rooms" : {
    "-KD8ky8mUR2US3eFBxVR" : {
      "-KD8l9oNHEFll2mE29Uu" : {
        "a5b93dea-1202-47b6-9b62-56ef3e6e71bc" : true,
        "facebook:1016794731699551" : true
      }
    },
    "-KD972O2fD9gWPDtxra8" : {
      "-KD972syP1pwO6o-2QWL" : {
        "a5b93dea-1202-47b6-9b62-56ef3e6e71bc" : true,
        "facebook:1016794731699551" : true
      }
    },
    "-KD9BQFnmX8qIgL8hwRE" : {
      "-KD9BiSRSAivnJ0-swBi" : {
        "a5b93dea-1202-47b6-9b62-56ef3e6e71bc" : true,
        "facebook:1016794731699551" : true
      }
    },
    "-KD9FxO2zsxPlqcz4s-1" : {
      "-KD9FyES0uRh-wAYjooV" : {
        "a5b93dea-1202-47b6-9b62-56ef3e6e71bc" : true,
        "faf8f6e6-e64e-43ed-8948-0894f3164191" : true
      }
    },
    "-KD9pP3CEpGyHLGfuvtu" : {
      "-KD9pPihICvaBdOQLKZe" : {
        "a5b93dea-1202-47b6-9b62-56ef3e6e71bc" : true,
        "facebook:1016794731699551" : true
      }
    }
  },
  "users" : {
    "a5b93dea-1202-47b6-9b62-56ef3e6e71bc" : {
      "biographie" : "qsddqd",
      "employeur" : {
        "-KDFYrZCTllVOp14_kgL" : true
      },
      "nom" : "ahlooooo",
      "prenom" : "dfsdf",
      "provider" : "password",
      "role" : "architecte",
      "rooms" : {
        "-KD8ky8mUR2US3eFBxVR" : true,
        "-KD972O2fD9gWPDtxra8" : true,
        "-KD9BQFnmX8qIgL8hwRE" : true,
        "-KD9FxO2zsxPlqcz4s-1" : true,
        "-KD9pP3CEpGyHLGfuvtu" : true
      },
      "tasker" : {
        "-KDFYMIw7bM9HYWRdOOd" : true
      }
    },
    "aca3433d-ba20-40e5-9e68-54ae44dbbf99" : {
      "nom" : "zqeqsd",
      "prenom" : "sdqsd",
      "provider" : "password"
    },
    "b4d918ad-54e4-4c6e-8261-9f35df842889" : {
      "nom" : "dfqsdf",
      "prenom" : "dfsdf",
      "provider" : "password"
    },
    "facebook:1016794731699551" : {
      "biographie" : "d",
      "email" : "Blabla",
      "employeur" : {
        "-KDFYMIw7bM9HYWRdOOd" : true
      },
      "image" : "img",
      "nom" : "f",
      "prenom" : "f",
      "provider" : "fs",
      "role" : "salamitteu",
      "rooms" : {
        "-KD8ky8mUR2US3eFBxVR" : true,
        "-KD972O2fD9gWPDtxra8" : true,
        "-KD9BQFnmX8qIgL8hwRE" : true
      },
      "skills" : "",
      "tasker" : {
        "-KDFYrZCTllVOp14_kgL" : true
      }
    }
  }
}

0 个答案:

没有答案