简体   繁体   English

套接字未在angular-firebase上连接以获得实时更新

[英]Socket not connecting on angular-firebase for getting the real time update

Hello I am new to angular and firebase trying to connect my angular app to get the real time data updates, I implemented the process from this url - https://www.djamware.com/post/5bbf534580aca7466989441c/angular-6-firebase-tutorial-firestore-crud-web-application , I am getting the initial data when the page loads from getOrder() function of the service. 您好,我是angular和firebase的新手,尝试连接我的angular应用程序以获取实时数据更新,我从此url实现了该过程-https: //www.djamware.com/post/5bbf534580aca7466989441c/angular-6-firebase- tutorial-firestore-crud-web-application ,当页面从服务的getOrder()函数加载时,我正在获取初始数据。

in my app component - 在我的应用程序组件中-

import * as firebase from 'firebase';
import firestore from 'firebase/firestore';
import { environment } from '../environments/environment'; // here my configuration is

const settings = {timestampsInSnapshots: true}

ngOnInit(){
    firebase.initializeApp(environment.firebase);
    firebase.firestore().settings(settings);
}

in my firebase.service - 在我的firebase.service中-

ref = firebase.firestore().collection('orders');
getOrders(): Observable<any> {
      console.log('on get orders snapshot');
      return new Observable((observer) => {
        this.ref.onSnapshot((querySnapshot) => {
          let boards = [];
          querySnapshot.forEach((doc) => {
            let data = doc.data();
            observer.next({
              driver_status: data.driver_status,
              food_status: data.food_status,
            });
          });
          observer.next(boards);
        });
      });
    }

    getOrder(id: string): Observable<any> {
      return new Observable((observer) => {
        this.ref.doc('ref-'+id).get().then((doc) => {
          let data = doc.data();
          observer.next({
            driver_status: data.driver_status,
            food_status: data.food_status,
          });
        });
      });
    }

in my detail component - 在我的详细信息组件中-



displayedColumns = ['driver_status', 'food_status'];
dataSource = new OrderDataSource(this.fs);

public fireOrder: Observable<any[]>;
ngOnInit(){
this.fs.getOrder(this.order.id).subscribe(res =>{
  this.fireOrder = res;
});
}
--------------------------
export class OrderDataSource extends DataSource<any> {

  constructor(private fs: FirebaseService) {
    super();
    console.log('in OrderDataSource constructor');
    this.connect();
  }

  connect() {
    console.log('in connect');
    return this.fs.getOrders();
  }

  disconnect() {
  }
}

I am getting the initial data from the firebase but it is not connecting the socket to get the real time data. 我正在从Firebase获取初始数据,但是它没有连接套接字以获取实时数据。

The doc().get() returns a promise, not a stream (Observable). doc()。get()返回一个promise,而不是一个流(Observable)。 With a promise, you can take advantage of only getting data once if that's your goal. 有了承诺,如果这是您的目标,则可以利用仅获取一次数据的优势。 In order to first get the data as well as listen to changes, what you need to be using is the doc().onSnapshot() method . 为了首先获取数据并聆听更改,您需要使用doc()。onSnapshot()方法

As a side mention, I'd recommend trying Angular's @angular/fire package (angular/angularfire2) for Angular + Firebase development since there is a lot of official documentation, specific support, and questions already out there for it. 顺便提一句,我建议您尝试Angular的@ angular / fire软件包 (angular / angularfire2)进行Angular + Firebase开发,因为已经有了很多官方文档,特定的支持和问题。 With it, it's possible for the getOrder method to be reduced to: 使用它,可以将getOrder方法简化为:

getOrder(id: string): Observable<Order> {
    console.log('on get order snapshot');
    return db.doc<Order>(`orders/${id}).valueChanges();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM