小编sat*_*ime的帖子

NestJs 在自定义验证器(类验证器)中获取请求实例或执行上下文

是否可以在 nestJs(类验证器 => 自定义验证器)中注入执行上下文或访问当前请求?

import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';
import { Injectable } from '@nestjs/common';
import { Connection } from 'typeorm';
import { InjectConnection } from '@nestjs/typeorm';

@ValidatorConstraint({ name: 'modelExistsConstraint', async: true })
@Injectable()
export class ModelExistsConstraint implements ValidatorConstraintInterface {

  constructor(
    @InjectConnection('default') private readonly connection: Connection,
  ) {
  }

  async validate(value: string, validationArguments: ValidationArguments) {
    // here need request or execution context;
    // const test = this.context.switchToHttp().getRequest();
    const model = await this.connection
      .getRepository(validationArguments.constraints[0])
      .findOne({ where: { [validationArguments.property]: …
Run Code Online (Sandbox Code Playgroud)

typescript class-validator nestjs

8
推荐指数
1
解决办法
1635
查看次数

传递给“selectId”实现的实体返回未定义。您可能应该提供自己的“selectId”实现

警告就在这里

在此输入图像描述

https://github.com/rokudo5262/phone这是我的项目,
我想加载智能表中的品牌列表,但收到警告,
我尝试修复,但警告仍然存在,请帮助
Brands-features.selector.ts

import { createFeatureSelector } from '@ngrx/store';
import { State, FeatureKey } from '../reducers';

export const selectBrandsState = createFeatureSelector<State>(FeatureKey);
Run Code Online (Sandbox Code Playgroud)

品牌.selector.ts

import { createSelector } from '@ngrx/store';
import { selectBrandsState } from './brands-features.selector';
import { BrandsReducer } from '../reducers';
import { brandAdapter } from '../states/brands.state';

export const selectBrandEntitiesState = createSelector(
  selectBrandsState,
  state => state[BrandsReducer.brandsFeatureKey],
);

export const {
  selectIds: selectBrandIds,
  selectEntities: selectBrandEntities,
  selectAll: selectAllBrands,
  selectTotal: selectTotalBrands,
} = brandAdapter.getSelectors(selectBrandEntitiesState);

export const BrandSelectors = {
  selectBrandEntitiesState,
  selectBrandIds,
  selectBrandEntities, …
Run Code Online (Sandbox Code Playgroud)

ngrx ngrx-effects angular ngrx-store ngrx-entity

6
推荐指数
2
解决办法
9155
查看次数

解析错误:由“&lt;!DOCTYPE html&gt;”引起的意外标记更漂亮/更漂亮

我有一个使用 vue cli 创建的 vue 应用程序,我使用的版本是 vue2(带有 eslint 和 prettier)。

我可以运行npm run serve并加载我的页面。但是在 Visual Studio Code 中,我注意到这个错误:

{
    "resource": "/c:/vue/app2/public/index.html",
    "owner": "eslint",
    "code": {
        "value": "prettier/prettier",
        "target": {
            "$mid": 1,
            "external": "https://github.com/prettier/eslint-plugin-prettier#options",
            "path": "/prettier/eslint-plugin-prettier",
            "scheme": "https",
            "authority": "github.com",
            "fragment": "options"
        }
    },
    "severity": 4,
    "message": "Parsing error: Unexpected token",
    "source": "eslint",
    "startLineNumber": 1,
    "startColumn": 2,
    "endLineNumber": 1,
    "endColumn": 2
}
Run Code Online (Sandbox Code Playgroud)

这是我.eslintrc.js创建应用程序时自动生成的,此后我没有对其进行任何更改。

module.exports = {  
  root: true,
  env: {
    node: true
  },
  extends: ["plugin:vue/essential", "eslint:recommended", "@vue/prettier"],
  parserOptions: …
Run Code Online (Sandbox Code Playgroud)

eslint prettier prettier-eslint

6
推荐指数
1
解决办法
2168
查看次数

是否可以使用 ng-mocks 保留模拟服务中的一些方法?

我正在向一个有角度的项目添加测试。有一项服务从 API 获取一些数据,并根据这些数据创建一些业务实体。

我使用ng-mocks模拟了此服务来测试依赖于它的组件。

原来的服务是这样的:

class DataService {
  public getEntityFromApi(): Observable<Entity> {
    return http.get(...).pipe(
      map(httpResponse => {
        return this.createEntityFromApiData(httpResponse);
      })
    );
  }

  private createEntityFromApiData(apiData: any): Entity {
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)

它被嘲笑为:

getMockedServiceProvider() {
  const testData = ...;
  return MockProvider(DataService, {
    getEntityFromApi: () => {
      let entity = // duplicated code from the createEntityFromApiData method to create the object from testData
      return of(entity);
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

因此,模拟服务能够返回一个对象,而无需向 API 发出请求,但我必须复制从纯 json 创建对象的代码。

避免模​​拟服务中出现重复的最佳方法是什么?

  1. 是否有可能以某种方式在被模拟的服务中保留createEntityFromApiData原始服务的私有性?
  2. 我是否应该将此逻辑隔离在另一个仅负责实例化的服务中,我将在测试中按原样使用该实例?
  3. 其他方法?

testing angular ng-mocks

5
推荐指数
1
解决办法
626
查看次数

如何在 ngrx 单元测试中更新模拟存储中的状态?

我有一个对象定义为:

obj:{
  name:"string",
  system:"string"
}
Run Code Online (Sandbox Code Playgroud)

但是我正在尝试将模拟商店中的值更新为:

store.setState({
  name:"Rose",
  system:"Updated Dummy"
});
store.refreshState();
fixture.detectChanges();
Run Code Online (Sandbox Code Playgroud)

但该值没有更新。如何更新该值,然后验证它是否已作为单元测试的一部分进行更新?

unit-testing ngrx angular

3
推荐指数
1
解决办法
2891
查看次数

如何修复:ngRx 中的“类型错误:实体不可迭代”

如何修复:ngRx 中的“类型错误:实体不可迭代”:如果有人明白这一点,请告诉我。

//用户.action

        import { Course } from '../model/user';
        import { createAction, props } from '@ngrx/store';
        import { Update } from '@ngrx/entity';

        export const loadCourses = createAction(
          '[Courses List] Load Courses via Service'
        );

        export const coursesLoaded = createAction(
          '[Courses Effect] Courses Loaded Successfully',
          props<{ courses: Course[] }>()
        );

        export const createCourse = createAction(
          '[Create Course Component] Create Course',
          props<{ course: Course }>()
        );

        export const deleteCourse = createAction(
          '[Courses List Operations] Delete Course',
          props<{ courseId: string }>()
        );

        export …
Run Code Online (Sandbox Code Playgroud)

redux ngrx angular

2
推荐指数
1
解决办法
6972
查看次数