firebase快照更改和angular2

时间:2016-03-30 14:56:12

标签: firebase angular

我遇到了firebase和angular2的小问题。我正在加载一些小任务,用户可以申请。

loadTask() {
  let post = []
  const publicationRef = this.rootRef.child('task').child('attente');
  const usersR = this.rootRef.child('users')
return Observable.create((observer) => {
  this.geoQuery.on("key_entered", function(key, location, distance){
    publicationRef.child(key).on('value', snapshot => {
      const taskkey = snapshot.key()
      snapshot.forEach((data) => {
        const datakey = data.key();
        const dataVal = data.val();
        usersR.orderByKey().equalTo(dataVal.user).on('child_added', snapshot => {
          console.log({key: datakey,nom: snapshot.val().nom, distances: distance})
           post.push({nom: dataVal.nom, key: datakey,unom: snapshot.val().nom, uprenom: snapshot.val().prenom, distance: Math.floor(distance), uimage:snapshot.val().image, budget: dataVal.budget, description: dataVal.description, date:dataVal.date, location: dataVal.location, user: dataVal.user, id1: taskkey, id:datakey})
          observer.next(post)
        })
      })
    })
  })
})
}

显示所有任务但是当我应用于任务时,在dom中它调用一个新的推送方法,因此当我重新加载页面时,会出现具有相同值的新任务并解除隔离。我试图删除推送方法

loadTask() {
  const publicationRef = this.rootRef.child('task').child('attente');
  const usersR = this.rootRef.child('users')
return Observable.create((observer) => {
  this.geoQuery.on("key_entered", function(key, location, distance){
    publicationRef.child(key).on('value', snapshot => {
      const taskkey = snapshot.key()
      snapshot.forEach((data) => {
        const datakey = data.key();
        const dataVal = data.val();
        usersR.orderByKey().equalTo(dataVal.user).on('child_added', snapshot => {
          console.log({key: datakey,nom: snapshot.val().nom, distances: distance})
          const publicio = [{nom: dataVal.nom, key: datakey,unom: snapshot.val().nom, uprenom: snapshot.val().prenom, distance: Math.floor(distance), uimage:snapshot.val().image, budget: dataVal.budget, description: dataVal.description, date:dataVal.date, location: dataVal.location, user: dataVal.user, id1: taskkey, id:datakey}]
          observer.next(publicio)
        })
      })
    })
  })
})
}

应用于任务不会添加具有相同值的新数据,但在dom中它只加载一组数据而不是所有数据

1 个答案:

答案 0 :(得分:0)

尝试清除" on"方法:

publicationRef.child(key).on('value', snapshot => {
  post = [];
  const taskkey = snapshot.key();
  .
  .
  .