Bird
0
0

Identify the mistake in this FileValidationPipe snippet:

medium📝 Debug Q6 of 15
NestJS - Pipes
Identify the mistake in this FileValidationPipe snippet:
transform(file) {
  const allowedExtensions = ['jpg', 'png'];
  const extension = file.originalname.split('.').pop;
  if (!allowedExtensions.includes(extension)) {
    throw new BadRequestException('Unsupported file type');
  }
  return file;
}
AThe allowedExtensions array should include uppercase extensions.
BThe <code>pop</code> method is not called as a function.
CThe pipe should check <code>file.mimetype</code> instead of extension.
DThe pipe should return null instead of the file.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the extension extraction

    The code uses file.originalname.split('.').pop but pop is a method and must be called as pop().
  2. Step 2: Effect of missing parentheses

    Without parentheses, extension is assigned the function reference, not the string extension.
  3. Step 3: Correct usage

    Change to file.originalname.split('.').pop() to get the file extension string.
  4. Final Answer:

    The pop method is not called as a function. -> Option B
  5. Quick Check:

    Remember to call array methods with parentheses [OK]
Quick Trick: Call pop() with parentheses to get last array element [OK]
Common Mistakes:
  • Forgetting parentheses on pop()
  • Ignoring case sensitivity in extensions
  • Checking mimetype instead of extension (not always wrong but different approach)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes