卸载组件

时间:2019-05-02 13:47:19

标签: javascript android reactjs react-native

在我的代码中,我对卸载组件有问题。特别是当我单击“通知”时,我得到一些值,而当我单击“取消订阅”时,它应该卸载通知已保存的内容。 但是我遇到了这样的错误:“ 未定义不是对象(正在评估“ p.default.unregisterNotification(o.notifyHandle).catch”)

我使用.remove()命令,但是它不起作用。

第1页

if (property.indexOf('Notify') === 0 || property.indexOf('Indicate') === 0) {
            if (!this.notifyHandle) {
                BleHelper.registerNotification(peripheral.id, serviceUuid, char.characteristic, this._onCharValueUpdate)
                    .then(handle => {
                        this.notifyHandle = handle;
                        this.forceUpdate();
                    })
                    .catch(err => {
                        console.warn(err)
                        ErrorRegistry.putError('GATT Register Notification', err);
                    });
            } else {
                BleHelper.unregisterNotification(this.notifyHandle)
                    .catch(err => {
                        console.warn(err)
                        ErrorRegistry.putError('GATT Unregister Notification', err);
                    })
                this.notifyHandle = null;
                this.forceUpdate();
            }

第2页

registerNotification(peripheralId, serviceId, charId, callback) {
        return new Promise(
            (resolve, reject) => {
                BleManager.startNotification(peripheralId, serviceId, charId)
                    .then(() => {
                        resolve(bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', callback));
                    })
                    .catch(err => {
                        console.warn('BleHelper registerNotification', err);
                        reject(err)
                    })
            }
        )
    }

    unregisterNotification(handle) {
        handle.remove();
    }

编辑

我尝试过这种方式:

unregisterNotification(handle) {
      return new Promise(
        (resolve, reject) => {
          BleManager.unregisterNotification(this.notifyHandle)
          .then(() => {
            resolve(handle.remove());
          })
          .catch(err => {
            console.warn('Error', err);
            reject(err)
          })
          }
      )
    }

现在我没有收到错误,但是单击通知时我没有删除该值。

1 个答案:

答案 0 :(得分:0)

BleHelper.unregisterNotification(this.notifyHandle)
  .catch(err => {
    console.warn(err)
    ErrorRegistry.putError('GATT Unregister Notification', err);
  })

// ...

  unregisterNotification(handle) {
    handle.remove();
  }

unregisterNotification不返回任何内容,因此无法在未定义的情况下调用.catch。要么使unregisterNotification返回承诺,要么删除.catch

相关问题