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
。您的 promise 方法并未保留 this
的值。您可以通过将代码更改为:
const fastPutProm = Promise.promisify(sftp.fastPut, {context: sftp});
关于javascript - Promise.promisify 后无法读取未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577330/