BooksServicebooks with three book titles as stringsgetAllBooks() that returns the books arrayBooksServicebooks with three book titles as stringsgetAllBooks() that returns the books arrayBooksService and inside it, create a private property called books that is an array containing these exact strings: 'The Hobbit', '1984', 'Brave New World'.Remember to use private books = [...] inside the class.
BooksService class, add a method called getAllBooks() that returns the books array.The method should return this.books.
Injectable from @nestjs/common and add the @Injectable() decorator above the BooksService class.Use @Injectable() just before the class declaration.
BooksService class so it can be used in other parts of the application.Use export class BooksService instead of just class BooksService.
What is the main purpose of creating a service in a NestJS application?
Which decorator is used to make a class injectable as a service in NestJS?
@Injectable() marks a class as a provider that can be injected.@Controller() is for controllers, @Module() for modules, and @Service() does not exist in NestJS.Given the following service code, what will be the output when getHello() is called?
import { Injectable } from '@nestjs/common';
@Injectable()
export class HelloService {
getHello(): string {
return 'Hello, NestJS!';
}
}getHello() returns exactly the string defined, no errors or undefined.Identify the error in this service code snippet:
import { Injectable } from '@nestjs/common';
@Injectable()
export class MathService {
add(a: number, b: number) {
return a + b
}
}add correctly takes two numbers and returns their sum. Semicolons are optional in TypeScript.You want to create a service that provides a list of users and inject it into a controller. Which of the following is the correct way to create and inject the service?
1. Create service with @Injectable()
2. Define a methodgetUsers()returning an array
3. Inject service in controller constructor
4. Use service method inside controller method
Pass the test and this pattern moves unknown