我按照https://docs.nestjs.com/techniques/mongodb 中的示例进行操作
问题是出现猫鼬验证错误时(例如,我有一个带有必填字段的架构,但未提供):
来自 games.service.ts:
async create(createGameDto: CreateGameDto): Promise<IGame> {
const createdGame = new this.gameModel(createGameDto);
return await createdGame.save();
}
Run Code Online (Sandbox Code Playgroud)
save() 函数返回一个 Promise。
现在我在 game.controller.ts 中有这个
@Post()
async create(@Body() createGameDto: CreateGameDto) {
this.gamesService.create(createGameDto);
}
Run Code Online (Sandbox Code Playgroud)
处理错误然后返回具有不同 http 状态和 json 文本的响应的最佳方法是什么?你通常会抛出一个HttpException但从哪里?如果我在承诺中使用 .catch() 处理错误,我就不能这样做。
(刚开始使用nestjs框架)
创建新用户将忽略来自 create-user.dto.ts
但是,当我更新用户时,它会添加不需要的字段,如下所示:
// update-user.dto.ts
import { IsEmail } from 'class-validator';
import { Address } from '../model/address';
export class UpdateUserDto {
firstName: string;
lastName: string;
@IsEmail(undefined, { message: 'Not a valid e-mail' })
email: string;
username: string;
password: string;
addresses: Address[];
}
Run Code Online (Sandbox Code Playgroud)
这是来自用户服务的更新操作
// user.service.ts
async update(data: UpdateUserDto) {
try {
this.logger.log(data);
const id = '5c6dd9852d4f441638c2df86';
const user = await this.userRepository.update(id, data);
return { message: 'Updated your information' };
} catch (error) {
this.logger.log(error);
throw new HttpException('', HttpStatus.INTERNAL_SERVER_ERROR);
}
} …Run Code Online (Sandbox Code Playgroud) 我想对请求有效负载应用验证,例如有字符串类型的字段名称。但名称不是必填字段,但如果存在,则必须执行@IsNotEmpty()
我试过这样的事情
@IsNotEmpty() name?: string//它不考虑?可选约束
在下面的代码中,我的测试用例按预期通过,但我使用 stryker 进行突变测试,handleError 函数在突变测试中幸存下来,所以我想通过测试是否调用 handleError 函数来杀死突变体。需要帮忙测试私有函数。
我试过 spyOn 但没有用
const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
Run Code Online (Sandbox Code Playgroud)
const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
Run Code Online (Sandbox Code Playgroud)
我尝试用对象数组映射一个键字符串。
我可以创建一个简单的对象,但我想在这些数组中轻松添加一个对象。地图对象非常适合执行此操作。
问题:我不知道如何为 GraphQL 定义类型映射 :'(
@ObjectType()
export class Inventaire
@Field()
_id: string;
@Field()
stocks: Map<string, Article[]>;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试对使用弹性搜索的服务进行单元测试。我想确保我使用了正确的技术。
我是这个问题许多领域的新用户,所以我的大部分尝试都是通过阅读与此类似的其他问题并尝试在我的用例中有意义的问题。我相信我缺少 createTestingModule 中的一个字段。也有时我看到providers: [Service]和其他人components: [Service]。
const module: TestingModule = await Test.createTestingModule({
providers: [PoolJobService],
}).compile()
Run Code Online (Sandbox Code Playgroud)
这是我当前的错误:
Nest can't resolve dependencies of the PoolJobService (?).
Please make sure that the argument at index [0]
is available in the _RootTestModule context.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
池作业服务
Nest can't resolve dependencies of the PoolJobService (?).
Please make sure that the argument at index [0]
is available in the _RootTestModule context.
Run Code Online (Sandbox Code Playgroud)
PoolJobService.spec.ts
import { Injectable } from '@nestjs/common'
import { ElasticSearchService } from …Run Code Online (Sandbox Code Playgroud) 我正在从sequelizeORM迁移到typeORM。在sequelize-cli 中,有一些很好的命令可以删除数据库并创建一个新数据库,例如:
node_modules/.bin/sequelize db:drop
node_modules/.bin/sequelize db:create
node_modules/.bin/sequelize db:migrate
Run Code Online (Sandbox Code Playgroud)
好的,对于 typeORM,我知道如何运行迁移,但我无法在任何地方找到如何自动创建或删除数据库。提前Tnx。
我想测试我的AuthModule控制器HTTP层与supertest作为官方描述Nestjs文档。此模块使用EmailService从EmailModule.
我知道您可以按如下方式覆盖提供程序:
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
AuthModule,
],
})
.overrideProvider(EmailService)
.useValue(buildMock(EmailService))
Run Code Online (Sandbox Code Playgroud)
但是,这并不工作,我假设,因为EmailModule在进口AuthModule。一种解决方法是.overrideProvider(...).useValue(...)在每一个供应商EmailModule,但这种非意义,因为我接下来要由进口也模拟模块EmailModule。
当我进行 e2e 测试时AuthModule,老实说,我并不关心它的EmailModule工作原理。我需要模拟的是EmailService并确保我的身份验证模块与该服务正确交互。
不幸的Test.createTestingModule({})是,没有.overrideModule()方法。
我试着EmailModule用开玩笑的方式嘲笑:
jest.mock('../../src/email/email.module.ts', () => {
@Module({})
class EmailModule {
}
return EmailModule;
});
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
Error: Nest cannot create the module instance. Often, this is because of …Run Code Online (Sandbox Code Playgroud) 感谢您过来寻求帮助。\n我有使用 NodeJs 的 API,\n它接受 3 个参数,但似乎验证存在问题。\n当我调用 API 时,它给了我这个结果,
\n\n[{\n "statusCode": 400,\n "message": [\n "an unknown value was passed to the validate function"\n ],\n "error": "Bad Request"\n}]\nRun Code Online (Sandbox Code Playgroud)\n帮助我,任何人都可以告诉我这是怎么回事!
\n我按顺序提出了三个论点,
\nhttps://www.youtube.com/watch?v=C5Y3fpHg45U1JP这是我的代码
\n /**\n * Gets Subtitle From Youtube\n * @param getYoutubeSubtitleDto\n */\n public async getSubtitle(\n getYoutubeSubtitleDto: GetYoutubeSubtitleDto,\n ): Promise<Subtitle[]> { \n const { videoId, lang, projectId } = getYoutubeSubtitleDto\n return this.getOrCreateSubtitles(videoId, lang, projectId)\n }\nRun Code Online (Sandbox Code Playgroud)\nexport class GetYoutubeSubtitleDto extends Video …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 postgres 中保存 jsonb 类型的对象数组
实体
@Column({type: 'jsonb', array: true, nullable: true})
testJson: object[];
Run Code Online (Sandbox Code Playgroud)
我在邮递员中发送的json
{
"testJson": [
{"skill": "docker", "experience": true},
{"skill": "kubernetes", "experience": false}
]
}
Run Code Online (Sandbox Code Playgroud)
我收到错误“格式错误的数组文字:”
另请告诉我是否可以查询此类数据类型?
nestjs ×10
node.js ×5
typeorm ×3
typescript ×3
graphql ×2
jestjs ×2
validation ×2
javascript ×1
mongodb ×1
postgresql ×1
testing ×1
typegraphql ×1