Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use cats.controller.ts in nestjs-mongodb-demo

Shulou Source: shulou.com Published: 2022-06-02 03:44:57 10月01日 Update

The use of cats.controller.ts in nestjs-mongodb-demo, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

/ / sec/app.module.tsimport {Module} from'@ nestjs/common';import {AppController} from'. / app.controller';import {AppService} from'. / app.service';import {MongooseModule} from'@ nestjs/mongoose'import {CatsModule} from'. / cats/cats.module' @ Module ({imports: [MongooseModule.forRoot ('mongodb://localhost/ajanuw', {useNewUrlParser: true}), / / name of ajanuw database CatsModule], controllers: [AppController], providers: [AppService],}) export class AppModule {} Model injection / / src/cats/schemas/cat.schema.tsimport * as mongoose from' mongoose';export const CatSchema = new mongoose.Schema ({name: String,}) Use import {Module} from'@ nestjs/common';import {CatsController} from'. / cats.controller';import {CatsService} from'. / cats.service' in cats.module.ts Import {MongooseModule} from'@ nestjs/mongoose'import {CatSchema} from'. / schemas/cat.schema'@Module ({imports: [MongooseModule.forFeature ([/ / Schema defines the structure of the database {name: 'Cat', schema: CatSchema} / / name:' Cat' cats table Cat must be the same as @ InjectModel ('Cat') in service]), controllers: [CatsController], providers: [CatsService]}) CatModelimport {Injectable} from' @ nestjs/common' is injected into export class CatsModule {} cats.service.ts Import {Model} from 'mongoose'import {InjectModel} from' @ nestjs/mongoose'import {Cat} from'. / interfaces/cat.interface'import {CreateCat} from'. / class/create-cat.class'const l = console.log@Injectable () export class CatsService {constructor (@ InjectModel ('Cat') private readonly catModel: Model / / Model can manipulate the data table) {} async create (cat: CreateCat): Promise {const createdCat = new this.catModel (cat) L (createdCat) / / {_ id: 5b8e2faba163251c9c769e6e Name: 'little black cat'} returns a docment return await createdCat.save () / / save document / / return this.catModel.insertMany (cat)} async findAll (): Promise {return await this.catModel.find (). Exec ()}} cats.controller.tsimport {Controller, Get, Post, Body} from'@ nestjs/common' Import {CatsService} from'. / cats.service'import {CreateCat} from'. / class/create-cat.class'const l = console.log;@Controller ('cats') export class CatsController {constructor (private readonly catsServer: CatsService) {} @ Post () create (@ Body () cat: CreateCat) {this.catsServer.create (cat);} @ Get () findAll () {return this.catsServer.findAll ()} CreateCatexport class CreateCat {readonly name: string } Catimport {Document} from 'mongoose'export interface Cat extends Document {readonly name: string;} complete cats.controller.tsimport {Controller, Get, Post, Body, Query, Delete} from' @ nestjs/common';import {CatsService} from'. / cats.service'import {CreateCat} from'. / class/create-cat.class'const l = console.log @ Controller ('cats') export class CatsController {constructor (private readonly catsServer: CatsService) {} @ Post () create (@ Body () cat: CreateCat) {this.catsServer.create (cat) @ Get () findAll () {return this.catsServer.findAll ()} @ Get ('find-cat') findCat (@ Query (' catName') catName) {return this.catsServer.findCat (catName)} @ Post ('update-cat-name') updateCatName (@ Body () body) {this.catsServer.updateCatName (body)} @ Delete (' remove-cat') removeCat (@ Body ('catName') catName) {this .catsServer.removeCat (catName)}} cats.service.tsimport {Injectable HttpException, HttpStatus} from'@ nestjs/common' Import {Model} from 'mongoose'import {InjectModel} from' @ nestjs/mongoose'import {Cat} from'. / interfaces/cat.interface'import {CreateCat} from'. / class/create-cat.class'const l = console.log;@Injectable () export class CatsService {constructor (@ InjectModel ('Cat') private readonly catModel: Model

< Cat >

) {} async create (cat: CreateCat): Promise

< Cat >

{/ / const createdCat = new this.catModel (cat) / / return await createdCat.save () return this.catModel.insertMany (cat)} async findAll (): Promise

< Cat[] >

{return await this.catModel.find () .exec ()} async findCat (name: string): Promise

< Cat[] >

{if (! name) {throw new HttpException ('request parameters are incorrect.', HttpStatus.FORBIDDEN)} let resCat = this.catModel.find ({name}) return resCat} async updateCatName ({name, newName}) {if (! name | |! newName) {throw new HttpException ('request parameters are incorrect.', HttpStatus.FORBIDDEN)} const where = {name: name} Const update = {$set: {name: newName}}; return await this.catModel.updateOne (where, update) } async removeCat (catName) {l (catName) if (! catName) {throw new HttpException ('request parameters are incorrect.', HttpStatus.FORBIDDEN)} return await this.catModel.deleteOne ({name: catName})}} after reading the above, have you mastered how to use cats.controller.ts in nestjs-mongodb-demo? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Methods parameters data usage content databases more problems helpless for this reason name this skills datasheets models articles experience structure industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei MySQL Shulou Technology Shulou Information