[ ] Regression
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
I followed the File Upload async configuration documentation
Multer options should work, uploading a file exceeding the limit should not work, and I should see the log from fileFilter
function each time I do an upload.
The app module app.module.ts
:
@Module({
imports: [
MulterModule.registerAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => ({
fileFilter: (req, file, cb) => {
console.log('********************', config.get('FILE_SIZE_LIMIT'));
cb(null, true);
},
limits: {
fileSize: config.get('FILE_SIZE_LIMIT'),
},
}),
inject: [ConfigService],
}),
]
})
The controller controller.ts
@Controller('upload')
export class FilesController {
constructor(private readonly config: ConfigService) {}
@Post()
@UseInterceptors(FileInterceptor('file'))
upload(@UploadedFile() file) {
console.log(file);
return 'Okay';
}
}
When having multer options as the second argument of FileInterceptor
everything works fine, but when using MulterModule
just like on the docs, the options are neglected.
Nest version: 6.1.1
For Tooling issues:
- Node version: 10.15.3
- Platform: Linux
Please, provide a minimal repository which reproduces your issue :)
I ran into a similar issue. I was trying to register multer in AppModule
, but expected the settings there to be used in a feature module. The multer options get consumed while the feature module is being created, so anything set in the AppModule
is ignored. Not sure if this is by design or a bug, but moving the multer module import to the feature module fixed it for me.
I am trying to do the same thing, except that I dont need 'config' module - in my case I need to query database and I want to use another module for that. But I cannot make it to work in any modules except AppModule, Nest produces error like this:
Nest can't resolve dependencies of the MULTER_MODULE_OPTIONS (?). Please make sure that the argument at index [0] is available in the MulterModule context. +424ms
Error: Nest can't resolve dependencies of the MULTER_MODULE_OPTIONS (?). Please make sure that the argument at index [0] is available in the MulterModule context.
import { Module } from '@nestjs/common';
import { UploadController } from './upload.controller';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { ProjectModule } from '../project/project.module';
import { ProjectService } from '../project/project.service';
@Module({
imports: [MulterModule.registerAsync({
useFactory: async (projectService: ProjectService) => ({
imports: [ProjectModule],
storage: diskStorage({
destination: async (req, file, cb) => {
const p = await projectService.findOne(req.params.id);
console.log(p);
//here we will get folder name from p...
return cb(null, `${__dirname}/../../data/project/${req.params.id}`)
},
filename: (req, file, cb) => {
return cb(null, file.originalname);
}
})
}),
inject: [ProjectService]
})],
controllers: [UploadController],
providers: [ProjectService],
})
export class UploadModule {}
I ran into a similar issue. I was trying to register multer in
AppModule
, but expected the settings there to be used in a feature module. The multer options get consumed while the feature module is being created, so anything set in theAppModule
is ignored. Not sure if this is by design or a bug, but moving the multer module import to the feature module fixed it for me.
Can we get an update on this? I just ran into it as an issue. Seems like this is something that you'd only want to define at the AppModule like with database and not need to copy into every module where you'll use multer?
Got it! Put the inject before the useFactory and voila, it works!
MulterModule.registerAsync({
imports: [ ConfigModule ],
inject: [ ConfigService ],
useFactory: async (configService: ConfigService) => {
return {
storage: diskStorage({
destination: async (req, file, cb) => {
const path: string = configService.get('staticPath');
console.log(path);
return cb(null, path);
},
filename: (req, file, cb) => {
return cb(null, `${Date.now()}_${file.originalname}`);
}
})
}
},
}),
I ran into a similar issue. I was trying to register multer in
AppModule
, but expected the settings there to be used in a feature module. The multer options get consumed while the feature module is being created, so anything set in theAppModule
is ignored. Not sure if this is by design or a bug, but moving the multer module import to the feature module fixed it for me.
Experiencing the same issue. Multer module registration ignored in the AppModule and works in the feature module. Is this a bug?
Please, check the documentation. MulterModule
is not a global module https://docs.nestjs.com/modules
Most helpful comment
Can we get an update on this? I just ran into it as an issue. Seems like this is something that you'd only want to define at the AppModule like with database and not need to copy into every module where you'll use multer?