Open soon530 opened 2 years ago
nestjs的要先裝
% yarn add @nestjs/mongoose
yarn add v1.22.19
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
warning " > @nestjs/mongoose@9.2.1" has unmet peer dependency "mongoose@^6.0.2".
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @nestjs/mongoose@9.2.1
info All dependencies
└─ @nestjs/mongoose@9.2.1
✨ Done in 2.90s.
再來是nodejs的套件
% yarn add mongoose
yarn add v1.22.19
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
success Saved 31 new dependencies.
info Direct dependencies
└─ mongoose@6.7.2
info All dependencies
├─ @aws-crypto/ie11-detection@2.0.2
├─ @aws-crypto/supports-web-crypto@2.0.2
├─ @aws-crypto/util@2.0.2
├─ @aws-sdk/abort-controller@3.201.0
├─ @aws-sdk/credential-provider-cognito-identity@3.204.0
├─ @aws-sdk/credential-providers@3.204.0
├─ @aws-sdk/middleware-sdk-sts@3.201.0
├─ @aws-sdk/querystring-parser@3.201.0
├─ @aws-sdk/service-error-classification@3.201.0
├─ @aws-sdk/util-hex-encoding@3.201.0
├─ @aws-sdk/util-locate-window@3.201.0
├─ @types/webidl-conversions@7.0.0
├─ @types/whatwg-url@8.2.2
├─ denque@2.1.0
├─ fast-xml-parser@4.0.11
├─ ip@2.0.0
├─ kareem@2.4.1
├─ memory-pager@1.5.0
├─ mongodb-connection-string-url@2.5.4
├─ mongodb@4.11.0
├─ mongoose@6.7.2
├─ mpath@0.9.0
├─ mquery@4.0.3
├─ saslprep@1.0.3
├─ sift@16.0.1
├─ smart-buffer@4.2.0
├─ socks@2.7.1
├─ sparse-bitfield@3.0.3
├─ strnum@1.0.5
├─ tr46@3.0.0
└─ whatwg-url@11.0.0
✨ Done in 9.64s.
來試一下簡易指令,哈哈。
% nest g mo features/user
CREATE src/features/user/user.module.ts (81 bytes)
UPDATE src/app.module.ts (552 bytes)
自動產生,但沒增加什麼內容。 features/user/user.module.ts
import { Module } from '@nestjs/common';
@Module({})
export class UserModule {}
自動幫忙更新。 app.module.ts
@Module({
imports: [HttpModule, MongooseModule.forRoot('mongodb://localhost:27017/vic'), UserModule],
controllers: [AppController, TodoController],
providers: [AppService],
})
也是簡化指令。
% nest g co features/user
CREATE src/features/user/user.controller.spec.ts (478 bytes)
CREATE src/features/user/user.controller.ts (97 bytes)
UPDATE src/features/user/user.module.ts (166 bytes)
也是沒什麼內容。
import { Controller } from '@nestjs/common';
@Controller('user')
export class UserController {}
把UserController加到UserModule去。
import { Module } from '@nestjs/common';
import { UserController } from './user.controller';
@Module({
controllers: [UserController]
})
export class UserModule {}
也是簡化指令。
% nest g s features/user
CREATE src/features/user/user.service.spec.ts (446 bytes)
CREATE src/features/user/user.service.ts (88 bytes)
UPDATE src/features/user/user.module.ts (240 bytes)
也是沒什麼內容。
import { Injectable } from '@nestjs/common';
@Injectable()
export class UserService {}
把UserService加到UserModule去。
import { Module } from '@nestjs/common';
import { UserController } from './user.controller';
import { UserService } from './user.service';
@Module({
controllers: [UserController],
providers: [UserService]
})
export class UserModule {}
_id少一碼會出錯,看起來_id長度有固定?
[Nest] 9664 - 11/09/2022, 1:59:40 PM ERROR [ExceptionsHandler] Cast to ObjectId failed for value "636b402669b452d11440d56" (type string) at path "_id" for model "User"
CastError: Cast to ObjectId failed for value "636b402669b452d11440d56" (type string) at path "_id" for model "User"
at model.Query.exec (/Users/vic/dev/nestjs-study/node_modules/mongoose/lib/query.js:4891:21)
at model.Query.Query.then (/Users/vic/dev/nestjs-study/node_modules/mongoose/lib/query.js:4990:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
https://ithelp.ithome.com.tw/articles/10278750
MongooseModule -> mongoose