0
0
NestJSframework~10 mins

Why providers encapsulate business logic in NestJS - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to inject a provider into a controller.

NestJS
constructor(private readonly [1]: UsersService) {}
Drag options to blanks, or click blank then click option'
AUsersService
BuserService
CusersService
DUserService
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name with uppercase as the property name
Misspelling the service name
2fill in blank
medium

Complete the code to call a method from the provider inside a controller method.

NestJS
return this.usersService.[1](id);
Drag options to blanks, or click blank then click option'
AfindUserById
BcreateUser
CdeleteUser
DupdateUser
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that creates or deletes instead of fetching
Using a method name that does not exist in the provider
3fill in blank
hard

Fix the error in the provider method signature to properly return a Promise.

NestJS
async getUser(id: string): [1] {
Drag options to blanks, or click blank then click option'
AObservable<User>
BUser
Cvoid
DPromise<User>
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the type directly without Promise
Using void or Observable instead of Promise
4fill in blank
hard

Fill both blanks to create a provider method that filters users by age.

NestJS
getUsersByAge([1]: number): User[] {
  return this.users.filter(user => user.[2] === age);
}
Drag options to blanks, or click blank then click option'
Aage
BageLimit
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and property causing errors
Using undefined variable names
5fill in blank
hard

Fill all three blanks to create a provider method that returns a dictionary of user names keyed by their IDs for users older than 18.

NestJS
getAdultUserNames(): Record<string, string> {
  return this.users.reduce((acc, user) => {
    if (user.[1] > [2]) {
      acc[user.[3]] = user.name;
    }
    return acc;
  }, {});
}
Drag options to blanks, or click blank then click option'
Aage
B18
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong property for the key in the dictionary
Comparing with incorrect age value