NestJS Problems with StreamableFile, don't get a JSON Object

Viewed 31

When trying to send a response within a promise with

(onrejected) => {
          return res.send(
            this.visualization.loadJsonData(
              'src/data/Softwarecomponent/Chromium Sample Analysis.json',
            ),
          );
        },

my StreamableFile Object won't get serialized as a JSON Object. I just get a bunch of information. I am not sure why this happen

controller file:

  @Get()
  findSofwareComponentByProject(
    @Query('project') project: any,
    @Headers('API_KEY') apiKey: string,
    @Res() res: Response,
  ) {
    // throw new DataNotFoundException();
    const data = this.visualization.findSoftwareComponentByProject(
      apiKey,
      project,
    );

    data
      .then(
        (onfulfill) => {
          console.log('fullfilled 1');
        },
        (onrejected) => {
          return res.send(
            this.visualization.loadJsonData(
              'src/data/Softwarecomponent/Chromium Sample Analysis.json',
            ),
          );
        },
      )
      .catch((err) => console.log('Something is not working', err));

    return data;
  }

service file: visualization.service.ts

// Testing method to return JSON data
  loadJsonData(path: string) {
    const file = createReadStream(join(process.cwd(), path));
    return new StreamableFile(file);
  }

enter image description here

1 Answers

You shouldn't res.send(StreamableFile). The StreamableFile class is so you can return a stream and Nest will handle calling stream.pipe(res) for you. If you do want to use res directly, then do new StreamableFile().getStream().pipe(res)

Related