【发布时间】:2021-07-05 17:02:21
【问题描述】:
我正在将 Nestjs 与 Mongoose 一起使用,并且在使用此代码时面临一些挑战:
import { Injectable } from '@nestjs/common';
import { Key, KeyDocument } from '@app/mongo';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
@Injectable()
export class KeysService {
constructor(
@InjectModel(Key.name) private readonly KeysModel: Model<KeyDocument>
) {
this.test()
}
private async test(): Promise<void> {
await this.KeysModel
.find()
.cursor()
.eachAsync(async (key) => {
key.token = 'lllsdfsd';
await key.save()
})
}
}
不知何故,我的服务在.eachAsync 内看不到文档类型。
架构文件是 Nestjs 文档的典型文件:
import { Document } from "mongoose";
import { Prop, SchemaFactory } from '@nestjs/mongoose';
export type KeyDocument = Key & Document;
export class Key {
@Prop()
_id: string;
@Prop({ required: true })
secret: string;
@Prop()
token: string;
@Prop()
expired_in: number;
@Prop()
tags: string[];
}
export const KeysSchema = SchemaFactory.createForClass(Key);
为什么会出现这样的问题,如何解决?
我也尝试过制作自己的特殊界面。具有与架构完全相同的字段并将其添加到
export type KeyDocument = Key & Document<IKey>;,但它对我不起作用。
【问题讨论】:
标签: node.js mongodb typescript mongoose nestjs