Skip to content

Incorrect code example in documentation #8326

@NunoMCP

Description

@NunoMCP

Affected URL

https://nodejs.org/en/learn/getting-started/fetch#streaming-responses-with-undici

Describe the issue in detail:

The current code example tries to destructure the statusCode property directly on the Stream function return value, but the statusCode is located on the StreamFactoryData

const { statusCode } = await stream(
  url,
  {
    method: 'GET',
    headers: {
      'User-Agent': 'undici-stream-example',
      Accept: 'application/json',
    },
  },
  () => {
    let buffer = '';
    ...

It is also parsing the incomplete JSON during the stream which results in several SyntaxError: Unterminated string in JSON errors

try {
  const json = JSON.parse(buffer);
  console.log(
    'Repository Names:',
    json.map(repo => repo.name)
  );
  buffer = '';
} catch (error) {
  console.error('Error parsing JSON:', error);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    contentIssues/pr concerning contentlearnIssues/pr concerning the learn section

    Type

    No type

    Projects

    Status

    📋 Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions