Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
Angular 当firebase ref按时间戳排序时,可观察到触发两次_Angular_Firebase_Firebase Realtime Database_Chat_Ionic3 - Fatal编程技术网

Angular 当firebase ref按时间戳排序时,可观察到触发两次

Angular 当firebase ref按时间戳排序时,可观察到触发两次,angular,firebase,firebase-realtime-database,chat,ionic3,Angular,Firebase,Firebase Realtime Database,Chat,Ionic3,我正在使用firebase制作聊天应用程序。我在聊天中添加了一条新消息。我拥有的prb是,当我按时间戳排序消息时,消息会显示两次,即使保存消息功能只在时间上运行。 以下是可观察到的结果: return new Observable((observer) => { var result = []; var lastMessage; var order = 'msgId'; this.db.ref('/chats/' + channelId + '/messages').ord

我正在使用firebase制作聊天应用程序。我在聊天中添加了一条新消息。我拥有的prb是,当我按时间戳排序消息时,消息会显示两次,即使保存消息功能只在时间上运行。 以下是可观察到的结果:

return new Observable((observer) => {
  var result = [];
  var lastMessage;
  var order = 'msgId';
  this.db.ref('/chats/' + channelId + '/messages').orderByChild(order).startAt(lastMsgId)
   .once('value', snapshot => {
    snapshot.forEach((childSnapshot) => {
      result.push(childSnapshot.val())
      return true;
    })
    if(result.length)
      lastMsgId = result[result.length-1].msgId + 1;
      observer.next(result);
    })
})
保存到数据库功能:

saveMessageToDatabase(currentUserId, otherUserId, channelId, message) {
    messageObj = {
      key: channelId,
      msgId: lastActivity,
      message: message,
      uid: currentUserId,
      timestamp: lastActivity
    };
  this.db.ref('/chats/' + channelId + '/messages/').push(messageObj)
}
在您的服务中(推荐)/组件:

constructor(private angularFireDatabase: AngularFireDatabase) {}

// lastMsgId: number | string
OnGetMessages(channelId: string, order: string, lastMsgId: number){
    return this.angularFireDatabase
      .list(`/chats/${channelId}/messages `, 
      ref => ref.orderByChild(order).startAt(lastMsgId))
      .valueChanges();
}
myMessages: any
//if you used service you should provide it in constructor.
constructor(private firebaseService: FirebaseService) {}

ngOnInit(){
  // you can esle where in other function, ngOnInit used just for example.
  this.firebaseService.subscribe((msgs)=>{
      console.log(msgs);
      this.myMessages = msgs;
    })
}
在您的组件中:

constructor(private angularFireDatabase: AngularFireDatabase) {}

// lastMsgId: number | string
OnGetMessages(channelId: string, order: string, lastMsgId: number){
    return this.angularFireDatabase
      .list(`/chats/${channelId}/messages `, 
      ref => ref.orderByChild(order).startAt(lastMsgId))
      .valueChanges();
}
myMessages: any
//if you used service you should provide it in constructor.
constructor(private firebaseService: FirebaseService) {}

ngOnInit(){
  // you can esle where in other function, ngOnInit used just for example.
  this.firebaseService.subscribe((msgs)=>{
      console.log(msgs);
      this.myMessages = msgs;
    })
}
如果有帮助,请告诉我。祝你编码好运