小编ffx*_*292的帖子

即使 {timestamps: true},Mongoose/NestJs 也无法访问createdAt

我目前正在使用 Mongoose 和 NestJs,并且在访问createdAt属性方面遇到了一些困难。

这是我的 user.schema.ts

@Schema({ timestamps: true})
export class User {
  @Prop({ required: true })
  name!: string;

  @Prop({ required: true })
  email!: string;
}

export const UserSchema = SchemaFactory.createForClass(User);
Run Code Online (Sandbox Code Playgroud)

在我的 user.service.ts 中

public async getUser(
    id: string,
  ): Promise<User> {
    const user = await this.userModel.findOne({ id });

    if (!user) {
      throw new NotFoundException();
    }

    console.log(user.createdAt) // Property 'createdAt' does not exist on type 'User' .ts(2339)
  }
Run Code Online (Sandbox Code Playgroud)

所以基本上我已经将时间戳设置为 true 但我仍然无法访问createdAt属性。顺便说一句,我还有一个可以正常工作的自定义 id,因此请在我的 service.ts 中忽略它

我已经尝试设置@Prop() createdAt?: Date架构,但仍然不起作用。

我还使用 …

javascript mongoose mongodb typescript nestjs

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

Mongoose - 无法访问createdAt

我正在使用 Mongoose 创建一个 NestJs 应用程序,尽管我已将时间戳设置为 true,但目前尝试访问createdAt 时遇到问题,我的代码如下。

产品架构.ts

@Schema({timestamps: true})
export class Product extends Document {
  @Prop({ required: true })
  name!: string;
}
Run Code Online (Sandbox Code Playgroud)

产品.服务.ts

public async getProduct(name: string): Promise<void> {
    const existingProduct = await this.productModel.findOne({ name });

    if (!existingProduct) {
      throw new NotFoundException();
    }

    existingProduct.createdAt //Property 'createdAt' does not exist on type 'Product'
  }
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb express typescript nestjs

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

如何等待多个 Promise.all()

我正在使用一些旧代码,并尝试返回两个承诺数组的结果。

所以基本上主体包含firstsecond是 id 数组。我知道下面的代码适用于 1 到 5 个 id 的小数组,但如果我要做几百个,在我从两个 Promise 数组中获得数百个结果之前,解析是否会触发?

 const doSomething = (body) => {
    return new Promise((resolve, reject) => {
      const promisesOne = body.first.map((id) => 
        doSomethingOne(id)
      );

      const promisesTwo = body.second.map((id) => 
        doSomethingTwo(id)
      );

      let response = {
        first: {}
        second: {}
        headers: 'mock headers'
      };

      Promise.all(promisesOne).then((results) => {
        response.first = results;
      });

      Promise.all(promisesTwo).then((results) => {
        response.second = results;
      });

      resolve(response);
  });
};
Run Code Online (Sandbox Code Playgroud)

另外,我无法将其重构为 async/await,因为此代码库不使用它。

javascript promise es6-promise

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

玩笑 - 测试 createAsyncThunk

我目前在测试 createAsyncThunk 函数时遇到一些问题。

基本上功能是:

const myFunc = createAsyncThunk('returns ID', async (nameAndEmail) => {
  const response = await axios.post('/backendroute', nameAndEmail);

  return response.data.id;
};
Run Code Online (Sandbox Code Playgroud)

所以这个函数会将姓名和电子邮件发送到后端,后端返回一个 ID。

目前我的测试是:

test('returns ID when myFunc is called', async () => {
  const nameAndEmail = {
    name: 'John Smith',
    email: '123@123.com'
  };

  const mockThunk = store.dispatch(myFunc(nameAndEmail));

  expect(mockThunk).toHaveBeenCalledWith(nameAndEmail);
});
Run Code Online (Sandbox Code Playgroud)

问题是,当我测试这个时,收到的值是:

Matcher error: received value must be a mock or spy function
{"abort": [Function abort], "arg": {"email": "123@123.com", "name": "John Smith"}, "requestId": "123456789"}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我做错了什么吗?

javascript jestjs redux axios redux-toolkit

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