无法读取 Promise.promisify 之后未定义的 属性

Cannot read property of undefined after Promise.promisify

let nasPath = "";
return getFamInfo(args.familyID)
    .then(function (famInfo) {
        nasPath = //some code involving famInfo here
        return getSFTPConnection(config.nasSettings);
    }).then(function (sftp) {
      const fastPutProm = Promise.promisify(sftp.fastPut);
      return fastPutProm(config.jpgDirectory, nasPath, {});
    });

如果我在const fastPutProm = Promise.promisify(sftp.fastPut);之后打断点,fastPutProm就是一个三参数函数。但是当我尝试 运行 这段代码时,我得到一个 TypeError: Cannot read property 'fastPut' of undefined 错误。我在这里做错了什么?

该错误意味着您的 sftp 值为 undefined,因此当您尝试将 sftp.fastPut 传递给 promisify() 方法时,它会生成一个错误,因为您试图引用 undefined.fastPut 这是一个 TypeError.

因此,解决方案是倒退几步,找出为什么 sftp 中没有所需的值。


另一种可能性是错误来自模块内部,这是因为 sftp.fastPut 的实现引用了 this,它期望是 sftp。您的承诺方法没有保留 this 的值。您可以通过将代码更改为:

来解决此问题
const fastPutProm = Promise.promisify(sftp.fastPut, {context: sftp});