重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
nestjs-MongoDB-demo中cats.controller.ts的使用方法,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
成都创新互联是一家专注于成都网站设计、网站制作与策划设计,改则网站建设哪家好?成都创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:改则等地区。改则做网站价格咨询:13518219792
// sec/app.module.ts import { 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 }), // ajanuw数据库的名字 CatsModule ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
// src/cats/schemas/cat.schema.ts import * as mongoose from 'mongoose'; export const CatSchema = new mongoose.Schema({ name: String, });
import { Module } from '@nestjs/common'; import { CatsController } from './cats.controller'; import { CatsService } from './cats.service'; import { MongooseModule } from '@nestjs/mongoose' import { CatSchema } from './schemas/cat.schema' @Module({ imports: [ MongooseModule.forFeature([ // Schema 定义数据库的结构 { name: 'Cat', schema: CatSchema } // name: 'Cat' cats 表, Cat 必须和service时@InjectModel('Cat')的一样 ]) ], controllers: [CatsController], providers: [CatsService] }) export class CatsModule {}
import { Injectable } 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// Model 可以操作数据表 ){} async create(cat: CreateCat): Promise { const createdCat = new this.catModel(cat) l(createdCat) // { _id: 5b8e2faba163251c9c769e6e, name: '小黑猫' } 返回一个docment return await createdCat.save() // 保存 document // return this.catModel.insertMany(cat) } async findAll(): Promise { return await this.catModel.find().exec() } }
import { 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() } }
export class CreateCat { readonly name: string; }
import { Document } from 'mongoose' export interface Cat extends Document { readonly name: string; }
import { 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) } }
import { 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('请求参数不正确.', HttpStatus.FORBIDDEN) } let resCat = this.catModel.find({ name }) return resCat } async updateCatName({name, newName}) { if(!name || !newName){ throw new HttpException('请求参数不正确.', 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('请求参数不正确.', HttpStatus.FORBIDDEN) } return await this.catModel.deleteOne({name: catName}) } }
看完上述内容,你们掌握nestjs-mongodb-demo中cats.controller.ts的使用方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!