nestjs-mongodb-demo中cats.controller.ts的使用方法
发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,nestjs-mongodb-demo中cats.controller.ts的使用方法,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。//
千家信息网最后更新 2024年10月22日nestjs-mongodb-demo中cats.controller.ts的使用方法
nestjs-mongodb-demo中cats.controller.ts的使用方法,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
// 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 }), // ajanuw数据库的名字 CatsModule ], controllers: [AppController], providers: [AppService],})export class AppModule {}
模型注入
// src/cats/schemas/cat.schema.tsimport * as mongoose from 'mongoose';export const CatSchema = new mongoose.Schema({ name: String,});
cats.module.ts 中使用
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 {}
cats.service.ts 中注入 CatModel
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() }}
cats.controller.ts
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() }}
CreateCat
export class CreateCat { readonly name: string;}
Cat
import { Document } from 'mongoose'export interface Cat extends Document { readonly name: string;}
完整的 cats.controller.ts
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) }}
cats.service.ts
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的使用方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
方法
参数
数据
使用方法
内容
数据库
更多
问题
束手无策
为此
原因
名字
对此
技能
数据表
模型
篇文章
经验
结构
行业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库面试常见问题记录
居民网络安全排查
易语言 创建mdb数据库
软件开发企业环评报告
创建数据库要多久
连尚网络安全吗
数据库tpc-h测试结果
无线网络安全设置图
安全数据库引用的帐户
顺鸿网络技术有限公司
淮南bim软件开发工程
珠海瀛海软件开发有限公司
迅付上海造艺网络技术公司
数据库样本下载
网络安全的现状与展望
晋城网络技术哪家好
零基础怎么建造我的世界服务器
干妈是网络安全员
oracle数据库基本操作
网络安全技能大赛内容
怎么查看帝国的数据库
松江区自动化软件开发批量定制
软件开发有什么学校本科
大话手游新服务器
oracle单实例数据库
数据库表查出来再更新表
上海参考软件开发定制介绍
第一章 网络技术基础
金昌市政府网络安全宣传
ktv服务器硬盘坏了怎么办