简体   繁体   English

卸载组件

[英]Unmount a component

in my code I have a problem with Unmount a component. 在我的代码中我遇到卸载组件的问题。 In particular when i click on "notify" I get some values, and when I click on unsubscribe it should unmount what the notify has save. 特别是当我点击“通知”时,我得到一些值,当我点击取消订阅时,它应该卸载通知已保存的内容。 But I get this kind of error: " Undefined is not an Object (Evaluating "p.default.unregisterNotification (o.notifyHandle).catch") " 但是我得到了这样的错误:“ 未定义不是对象(评估”p.default.unregisterNotification(o.notifyHandle).catch“)

I use the command .remove() but it doesn't work. 我使用命令.remove()但它不起作用。

page1 第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();
            }

page 2 第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();
    }

EDIT 编辑

I have tried to do in this way: 我试图这样做:

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

Now I don't get the error but i don't remove the value when i click on notify. 现在我没有收到错误,但是当我点击通知时我没有删除该值。

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

// ...

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

unregisterNotification isn't returning anything, so you can't call .catch on undefined. unregisterNotification没有返回任何内容,所以你不能在undefined上调用.catch Either have unregisterNotification return a promise, or remove the .catch . 要么unregisterNotification返回一个promise,要么删除.catch

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

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