Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, {});
    });

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?

like image 672
AyushISM Avatar asked Jan 29 '26 15:01

AyushISM


1 Answers

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.

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


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});
like image 111
jfriend00 Avatar answered Jan 31 '26 03:01

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!