Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.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 检查firestore实时更改_Javascript_Node.js_Reactjs_React Native_Google Cloud Firestore - Fatal编程技术网

Javascript 检查firestore实时更改

Javascript 检查firestore实时更改,javascript,node.js,reactjs,react-native,google-cloud-firestore,Javascript,Node.js,Reactjs,React Native,Google Cloud Firestore,我正在地图项目中使用firestore real time,它需要以x距离间隔更新用户的当前位置。 但是,实时侦听器会不断重新读取我自己的更新,从而增加我的读取量。 我假设firestore实时更新缓存,然后再发送到服务器,是否可以忽略该用户所做的获取更改 class Booking extends Component { constructor(props) { super(props); this.state = { isLoading: false, e

我正在地图项目中使用firestore real time,它需要以x距离间隔更新用户的当前位置。 但是,实时侦听器会不断重新读取我自己的更新,从而增加我的读取量。 我假设firestore实时更新缓存,然后再发送到服务器,是否可以忽略该用户所做的获取更改

class Booking extends Component {
  constructor(props) {
   super(props);
   this.state = {
     isLoading: false,
     errorMessage: '',
   };
   this.unsubscribe = null;
 }

 componentDidMount() {
   this.getRealTimeData();
 }

 componentWillUnmount() {
    this.unsubscribe = null;
  }

 getRealTimeData = () => {
   this.fetchCompletedGigs();
 }

   fetchCompletedGigs = () => {
     const { userID } = this.props;
     this.props.bookingData([]);

     this.setState({ isLoading: true, errorMessage: '' });
     this.unsubscribe = Firebase.shared.fetchBooking('Bookings')
      .where('userID', '==', userID)
      .orderBy('d.CreatedAt', 'desc')
      .limit(20)
       .onSnapshot((querySnapshot) => {
         if (querySnapshot.empty) {
           this.setState({
             isLoading: false,
             errorMessage: "You currently don't have anybooking",
           });
           this.props.bookingData([]);
         }
         querySnapshot.docChanges().forEach(change => {
           const doc = change.doc;
           const item = doc.data();
           item.docId = doc.id;
           const list = [...this.props.bookings, item];
           this.setState({ isLoading: false, errorMessage: '' });
           if (change.type === 'added') {
             const filterList = _.uniqBy(list, 'docId');
             this.props.bookingData(filterList);
           } else if (change.type === 'removed') {
             const newData = list.filter(task => task.docId !== doc.id);
              const filterList = _.uniqBy(newData, 'docId');
             return this.props.bookingData(filterList);
           } else if (change.type === 'modified') {
             const newData = list.filter(task => task.docId !== doc.id);
             const newList = [...newData, item];
             const filterList = _.uniqBy(newList, 'docId');
             return this.props.bookingData(filterList);
           }
         }, err => {
           this.props.bookingData([]);
           console.warn(err);
            this.setState({
              isLoading: false,
              errorMessage: 'Error occurred while fetching your booking',
            });
          });
       }, err => {
         this.props.bookingData([]);
         console.warn(err);
          this.setState({
            isLoading: false,
            errorMessage: 'Error occurred while fetching your booking.',
          });
        });
   }

无法阻止
onSnapshot
侦听器为本地事件触发。但您可以在回调中检测这些本地事件,并在那里忽略它们:

Firebase.shared.fetchBooking('Bookings')
  .where('userID', '==', userID)
  .orderBy('d.CreatedAt', 'desc')
  .limit(20)
  .onSnapshot((querySnapshot) => {
     ...
     querySnapshot.docChanges().forEach(change => {
       if (change.doc.metadata.hasPendingWrites) {
         ... handle the local event differently
       }
       else {
         ... handle normally
       }
     });
     ...
  });

另请参阅上的Firebase文档。

能否发布您的侦听器的代码片段?请与我们分享一个示例代码。我现在也已经这样做了,我想知道我是否以正确的方式取消订阅?