#Error reading files in a folder:

1 messages · Page 1 of 1 (latest)

past remnant
#

So my pictures.service script below:

import { Controller, Get, Render } from '@nestjs/common';
import { PicturesService } from './pictures/pictures.service';
@Controller()
export class AppController {
  private readonly folderPath = '../public/images/perfumes'
  constructor(private readonly picturesService:PicturesService){}

  @Get()
  @Render('index')
  getHello() {
    const data = {
      title: 'Jadore',
      message: 'Welcome to my application!',
      images: this.picturesService.getImages(this.folderPath)
    };
    return data;
  }
  @Get('/contact')
  @Render('contact')
  getContact(){
    let data = {
      title: "Contact Page"
    }
    return data
  }
  
}

is later imported to controller:

import { Controller, Get, Render } from '@nestjs/common';
import { PicturesService } from './pictures/pictures.service';
@Controller()
export class AppController {
  private readonly folderPath = '../public/images/perfumes'
  constructor(private readonly picturesService:PicturesService){}

  @Get()
  @Render('index')
  getHello() {
    const data = {
      title: 'Jadore',
      message: 'Welcome to my application!',
      images: this.picturesService.getImages(this.folderPath)
    };
    return data;
  }
  @Get('/contact')
  @Render('contact')
  getContact(){
    let data = {
      title: "Contact Page"
    }
    return data
  }
  
}

and later renders onto the img tag in my index.hbs:

                {{#each images}}
                <img src="../public/images/perfumes/{{ this }}">
                {{/each}}

However I keep getting the error that the directory cannot be found:

Error reading files from the folder. Error: ENOENT: no such file or directory, scandir '../public/images/perfumes'
    at Object.readdirSync (node:fs:1452:3)
#

My file structure is as follows:

project-root
|-- src
|   |---pictures
|     |----pictures.service  
|   |-- app.controller.ts
|   |-- app.module.ts
|   |-- ...
|-- public
|   |-- images
|   |   |-- perfumes
|   |   |   |-- image1.jpg
|   |   |   |-- image2.jpg
|   |   |   |-- ...
|   |-- ...
|-- views
|   |-- index.hbs
|   |-- ...
|-- ...
past remnant
#

solved this by chagning the path directory: ```ts
private readonly folderPath = path.join(process.cwd(),'public/images/perfumes')

opal spireBOT
#

The most common error in Nest is failing to resolve provider dependencies.
The error message usually looks something like this:

Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> context.

Potential solutions:
- Is <module> a valid NestJS module?
- If <unknown_token> is a provider, is it part of the current <module>?
- If <unknown_token> is exported from a separate @Module, is that module imported within <module>?
  @Module({
    imports: [ /* the Module containing <unknown_token> */ ]
  })

The most common culprit of the error, is not having the <provider> in the module's providers array.
Please make sure that the provider is indeed in the providers array and following standard NestJS provider practices.

There are a few gotchas, that are common. One is putting a provider in an imports array.
If this is the case, the error will have the provider's name where <module> should be.

#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution if you are having the same issue, and then re-open the post if you are still having trouble, providing as much extra information as possible.