Typescript: Cannot read property 'headers' of undefined. Issue with reference to "this" in Body methods
See original GitHub issueHi there,
I am doing a new npm package using this library to scrape images with node js. Everything works fine when it comes to retrieve all image sources in the page, but there is some issue when I want to get the blob for a specific image(the goal is save this blob to a mongodb instance).
This is my function:
async (url: string): Promise<Blob> => {
if (url.indexOf('http') !== 0) url = 'http://' + url;
if (!checkValidUrl(url)) throw new Error('Image URL is not valid');
try {
const { blob } = await fetch(url);
const result = await blob();
return result as Blob;
} catch (e) {
throw e;
}
}
I tried it with Jest and - just to be sure - on a nodeJS server which is running using express but I always have the same result:
TypeError: Cannot read property ‘headers’ of undefined
40 | try {
41 | const { blob } = await fetch(url);
> 42 | const result = await blob();
| ^
43 | return result as Blob;
44 | } catch (e) {
45 | throw e;
**at blob (node_modules/node-fetch/lib/index.js:248:17) ==> this is the line with the reference to _this_ which cause the issue**
at src/index.ts:42:26
at step (src/index.ts:33:23)
at Object.next (src/index.ts:14:53)
at fulfilled (src/index.ts:5:58)
I am pretty sure it is not a blob related problem because I tried the functions text and json and there is almost the same problem, but on different properties: the reference to this in always undefined
Could you please guy point me in the right direction?
Thank you in advance
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Hi @xxczaki Thank you for taking your time. I know you closed the issue, but I update the package to version 3.0.0-beta.5 and I still have the same exact issue and I am stuck 😦 Could there be something wrong in my code? Thank you
EDIT: I managed to solve this by using promises in classic way instead of async/await and using require instead of import. It could be useful 😉
@matteotrapani the change was made at #810 it’s not released yet and beta5 includes old declarations. It should be part of Beta 6 that hopefully will be released soon.