Promise.promisify后无法读取undefined的属性

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);之后放置断点const fastPutProm = Promise.promisify(sftp.fastPut);fastPutProm是一个带三个参数的函数。 但是,当我尝试运行此代码时,我得到一个TypeError: Cannot read property 'fastPut' of undefined错误的TypeError: Cannot read property 'fastPut' of undefined 。 我在这里做错了什么?

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

所以,解决办法是备份几个步骤,并找出为什么sftp没有所需的值。


另一种可能性是错误来自模块内部,这是因为sftp.fastPut的实现引用了它期望的sftp 。 promisifying的方法不保存这个的价值。 你可以通过改变你的代码来解决这个问题:

 const fastPutProm = Promise.promisify(sftp.fastPut, {context: sftp});