[英]nestjs mongoose Type 'string' is not assignable to type 'Condition<LeanDocument<User>>
Im using nestjs with mongoose.我将nestjs与mongoose一起使用。 I try to do a simple find query我尝试做一个简单的查找查询
this.challengesModel.find({ createdBy: userId })
where this.challengesModel is injected like this this.challengesModel 像这样注入的地方
private readonly challengesModel: Model<Challenge>
but it says但它说
Type 'string' is not assignable to type 'Condition<LeanDocument<User>>'
createdBy is considered as a User object whereas I am giving it a string(userId) How can I still keep the createdBy field as User by search only by the id? createdBy 被认为是用户 object 而我给它一个字符串(userId) 我怎样才能通过仅通过 id 搜索将 createdBy 字段保留为用户?
This is my schema这是我的架构
@Schema()
export class Challenge extends Document {
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: "User" })
createdBy: User;
@Prop()
description: string;
@Prop({ default: new Date() })
creationTime: Date;
@Prop()
video: string;
@Prop({ default: [] })
likes: string[];
@Prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }] })
selectedFriends: User[];
@Prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: "Reply" }] })
replies: Reply[];
}
The createdBy is saved as a unique id of the user (foreign key to the users collection) I am trying to perform a query to find challenges that were created by a user with specific id. createdBy 保存为用户的唯一 ID(用户集合的外键)我正在尝试执行查询以查找由具有特定 ID 的用户创建的挑战。 If I change createdBy to be a string(id) it works, but then I don't get all the user properties, also the nest documentation suggests to create it like I did.如果我将 createdBy 更改为 string(id) 它可以工作,但是我没有获得所有用户属性,nest 文档也建议像我一样创建它。 What should I change in order to be able to do this find without any compliation errors?为了能够在没有任何编译错误的情况下进行此查找,我应该进行哪些更改?
Try尝试
this.challengesModel.find({ createdBy: userId as any })
This is caused because createdBy isn't a string type.这是因为 createdBy 不是字符串类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.