首页 > 解决方案 > 类型错误:unsubFirebaseSnapShot01 不是函数

问题描述

我正在使用 React 构建一个 Web 应用程序。

在主屏幕上,我有以下代码。运行应用程序时,它会抛出TypeError: unsubFirebaseSnapShot01 is not a function。

浏览器控制台错误截图:

这些是对这些函数的唯一引用index.js

let unsubFirebaseSnapShot01;
let unsubFirebaseSnapShot02;
let unsubFirebaseSnapShot03;

...

class Home extends Component {

...

  componentWillUnmount() {
    this.mounted = false;
    unsubFirebaseSnapShot01();
    unsubFirebaseSnapShot02();
    unsubFirebaseSnapShot03();
  }

  checkAll = () => {
    this.checkMeetingsSetByMe();
    this.checkMeetingsSetForMe();
    this.checkNotifications();
  };

  checkMeetingsSetByMe = () => {
    if (!this.mounted) return;
    const {
      user: { uid },
    } = this.props;
    this.setState({ screenLoading: true });

    unsubFirebaseSnapShot01 = Meetings.where('setBy', '==', uid).onSnapshot(
      ({ docs }) => {
        // setting all the meetingsSetByMe in one place
        const meetingsSetByMe = docs.map(item => item.data());
        if (this.mounted);
        this.setState({ meetingsSetByMe, screenLoading: false });
      },
    );
  };

  checkMeetingsSetForMe = () => {
    if (!this.mounted) return;
    const {
      user: { uid },
    } = this.props;
    this.setState({ screenLoading: true });

    unsubFirebaseSnapShot02 = Meetings.where('setWith', '==', uid).onSnapshot(
      ({ docs }) => {
        // setting all the meetingsSetForMe in one place
        const meetingsSetForMe = docs.map(item => item.data());
        if (this.mounted);
        this.setState({ meetingsSetForMe, screenLoading: false });
      },
    );
  };

  checkNotifications = () => {
    if (!this.mounted) return;
    const {
      user: { uid },
    } = this.props;
    this.setState({ screenLoading: true });

    unsubFirebaseSnapShot03 = Meetings.where('status', '==', 'unseen')
      .where('setWith', '==', uid)
      .onSnapshot(({ docs }) => {
        const notifications = docs.map(item => item.data());
        if (this.mounted);
        this.setState({ notifications, screenLoading: false });
      });
  };

仔细检查任何错别字,但看不到我的错误。有什么帮助吗?

标签: reactjs

解决方案


推荐阅读