Challenge - 5 Problems
NestJS CRUD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this NestJS controller method?
Consider this NestJS controller method that handles a GET request to fetch all items. What will be the HTTP response body when this method is called?
NestJS
import { Controller, Get } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get() findAll() { return ['apple', 'banana', 'cherry']; } }
Attempts:
2 left
💡 Hint
The method returns a simple array directly.
✗ Incorrect
The controller method returns an array, so NestJS sends it as JSON with status 200.
❓ state_output
intermediate2:00remaining
What is the value of the 'items' array after these service calls?
Given this NestJS service managing an in-memory list, what is the content of the 'items' array after executing the code below?
NestJS
export class ItemsService { private items = ['a', 'b']; addItem(item: string) { this.items.push(item); } removeItem(item: string) { this.items = this.items.filter(i => i !== item); } getItems() { return this.items; } } const service = new ItemsService(); service.addItem('c'); service.removeItem('a'); const result = service.getItems();
Attempts:
2 left
💡 Hint
Adding 'c' then removing 'a' affects the array.
✗ Incorrect
Initially ['a','b'], add 'c' → ['a','b','c'], remove 'a' → ['b','c'].
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in this NestJS controller method?
Identify the option that will cause a syntax error when defining a POST method in a NestJS controller.
NestJS
import { Controller, Post, Body } from '@nestjs/common'; @Controller('users') export class UsersController { @Post() createUser(@Body() userData) { return userData; } }
Attempts:
2 left
💡 Hint
Check the decorator syntax carefully.
✗ Incorrect
The @Body decorator requires parentheses when used as a decorator. Missing () causes syntax error.
🔧 Debug
advanced2:00remaining
Why does this NestJS service method throw a runtime error?
This service method is supposed to update an item by id. Why does it throw an error when called?
NestJS
export class ItemsService { private items = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]; updateItem(id: number, newName: string) { const item = this.items.find(i => i.id === id); item.name = newName; } } const service = new ItemsService(); service.updateItem(3, 'c');
Attempts:
2 left
💡 Hint
What happens if find() returns undefined?
✗ Incorrect
If no item matches id, find() returns undefined, so item.name causes TypeError.
🧠 Conceptual
expert2:00remaining
Which option correctly describes the role of DTOs in NestJS CRUD operations?
In NestJS, Data Transfer Objects (DTOs) are often used in CRUD operations. Which statement best describes their purpose?
Attempts:
2 left
💡 Hint
Think about how data is structured and validated in NestJS.
✗ Incorrect
DTOs specify data structure and validation rules to ensure safe and consistent data handling.