函数从 pouchDB 获取委托,插入一些调试数据和 return 一个承诺

function getting a delegate from pouchDB, insert some debug data and return a promise

我假装从 pouchDB 拦截函数并将其发送到日志 API。之后我会return一个承诺给任何需要使用的部分

我创建了一个接受委托和任何类型参数的函数。但是return我是不是报错信息:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property '_closed' of undefined
    TypeError: Cannot read property '_closed' of undefined
        at vendor.js:172457
        at vendor.js:81588
        at vendor.js:172415
        at new t (polyfills.js:3)
        at vendor.js:172402
        at vendor.js:81588
        at main.js:54
        at new t (polyfills.js:3)
        at LogSystemProvider.webpackJsonp.111.LogSystemProvider.interceptAndDebug (main.js:52)
        at DbService.webpackJsonp.43.DbService.bulkDocs (main.js:6141)

函数:

public interceptAndDebug(func: DelegateDTO): Promise<any>;
  public interceptAndDebug(func: DelegateDTO, param: any): Promise<any>;
  public interceptAndDebug(func: DelegateDTO, param?: any): Promise<any> {
    return new Promise((resolve, reject) => {
      if(param){
        func( param )
          .then((res)=>{

            let msg: ValueDebug = {
              nameFunc: func.name,
              param: param,
              sucess: res
            }

            this.CreateLog({
              msg: JSON.stringify(msg),
              isDebugger: true
            });

            resolve(res);
          })
          .catch((err)=>{

            let msg: ValueDebug = {
              nameFunc: func.name,
              param: param,
              error: err
            }

            this.CreateLog({
              msg: JSON.stringify(msg),
              isDebugger: true
            });

            reject(err)
          });
      }else{
        func()
          .then((res)=>{

            let msg: ValueDebug = {
              nameFunc: func.name,
              param: param,
              sucess: res
            }

            this.CreateLog({
              msg: JSON.stringify(msg),
              isDebugger: true
            });

            resolve(res);
          })
          .catch((err)=>{

            let msg: ValueDebug = {
              nameFunc: func.name,
              param: param,
              error: err
            }

            this.CreateLog({
              msg: JSON.stringify(msg),
              isDebugger: true
            });

            reject(err)
          });
      }


    });
  }

DelegateDTO:

export interface DelegateDTO {
  (any?): Promise<any>;
}

函数的使用:

bulkDocs( list: Array<any> ){
    return this._logSystem.interceptAndDebug(this._db.bulkDocs, list);
}

有人可以帮助我吗?

如果 bulkDocsthis._db 的方法,您可能需要绑定它以便它不会丢失 this._db 的上下文:

return this._logSystem.interceptAndDebug(this._db.bulkDocs.bind(this._db), list);