question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript: Cannot read property 'headers' of undefined. Issue with reference to "this" in Body methods

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
matteotrapanicommented, May 22, 2020

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 😉

0reactions
tinovyatkincommented, May 25, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'headers' of undefined on ...
At this moment I'm yet not saving the input data in the .json file. The problem I'm facing is I'm getting the next...
Read more >
How to Avoid the Infamous "Cannot read properties of ... - Bitovi
That error message is telling you the function is returning undefined implicitly, but its return type does not include undefined in it. Awesome!...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
Cannot read property 'headers' of undefined [resolved]
Hi! This error is caused by having an image url in your app that does not return appropriate headers, which then causes an...
Read more >
cannot read properties of undefined (reading 'data') axios
This error occurs when the Axios library is unable to find the data it is looking for. This can be caused by a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found