简体   繁体   English

nestjs mongoose 类型“字符串”不可分配给类型“条件” <leandocument<user> &gt; </leandocument<user>

[英]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.

相关问题 类型 'string' 不能分配给类型 'Condition&lt;..&gt;' 与 ts mongoose - Type 'string' is not assignable to type 'Condition<..>' with ts mongoose 输入'文件<any> ' 不可分配给类型 'Pick <pick<_leandocument<ticketdoc></pick<_leandocument<ticketdoc></any> - Type 'Document<any>' is not assignable to type 'Pick<Pick<_LeanDocument<TicketDoc> 类型“字符串”不可分配给类型“条件”<userobj> ' 当通过 ID 进行 mongoose 查询时</userobj> - Type 'string' is not assignable to type 'Condition<UserObj>' when making mongoose query by ID 'match' 类型的参数不可分配给 any[] 类型的参数 - nodejs / mongoose - Argument of type 'match' is not assignable to parameter of type any[] - nodejs / mongoose 这个错误说明了什么? 类型“ParsedQs”不可分配给类型“字符串” - What does this error say? Type 'ParsedQs' is not assignable to type 'string' 不能在 mongoose 中使用通用 model:“x”类型的参数不可分配给 MongooseFilterQuery 类型的参数 - Cannot use generic model in mongoose: Argument of type 'x' is not assignable to parameter of type MongooseFilterQuery 输入'{类型:任何; 必填:[真,字符串]; 独特的:真实的; 匹配:(字符串|正则表达式)[]; }' 不可分配给类型 'string' - Type '{ type: any; required: [true, string]; unique: true; match: (string | RegExp)[]; }' is not assignable to type 'string' 在nestjs中定义猫鼬实体Prop时Type[]和[Type]有什么区别 - What is the difference between Type[] and [Type] while defining mongoose entity Prop in nestjs 具有随机键但为String类型的对象的猫鼬模式 - Mongoose schema of an object with random keys but of String type 无法在 MEAN 堆栈中删除 - Angular 13 - 'string | 类型的参数 undefined' 不能分配给“string”类型的参数 - cannot delete in MEAN Stack - Angular 13 - Argument of type 'string | undefined' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM