Promise.promisify后无法读取未定义的属性 [英] Cannot read property of undefined after Promise.promisify

查看:174
本文介绍了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错误.我在这里做什么错了?

If I put a breakpoint after const fastPutProm = Promise.promisify(sftp.fastPut);, fastPutProm is a function with three arguments. But when I try to run this code, I get a TypeError: Cannot read property 'fastPut' of undefined error. What am I doing wrong here?

推荐答案

该错误表示您的sftp值为undefined,因此当您尝试将sftp.fastPut传递给promisify()方法时,它将生成错误因为您正在尝试引用TypeError,即TypeError.

That error means that your sftp value is undefined so when you try to pass sftp.fastPut to the promisify() method, it generates an error because you're trying to reference undefined.fastPut which is a TypeError.

因此,解决方案是备份一些步骤,弄清楚为什么sftp中没有所需的值.

So, the solution is to back up a few steps and figure out why sftp doesn't have the desired value in it.

另一种可能性是该错误来自模块内部,这是因为sftp.fastPut的实现正在引用它预期为sftpthis.您的承诺方法不是保留this的值.您可以通过将代码更改为以下内容来解决此问题:

Another possibility is that the error is coming from inside the module and it's because the implementation of sftp.fastPut is referencing this which it expects to be sftp. Your method of promisifying is not preserving the value of this. You can fix that by changing your code to:

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

这篇关于Promise.promisify后无法读取未定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆