[英]Type 'string' is not assignable to type 'Condition<..>' with ts mongoose
[英]nestjs mongoose Type 'string' is not assignable to type 'Condition<LeanDocument<User>>
我將nestjs與mongoose一起使用。 我嘗試做一個簡單的查找查詢
this.challengesModel.find({ createdBy: userId })
this.challengesModel 像這樣注入的地方
private readonly challengesModel: Model<Challenge>
但它說
Type 'string' is not assignable to type 'Condition<LeanDocument<User>>'
createdBy 被認為是用戶 object 而我給它一個字符串(userId) 我怎樣才能通過僅通過 id 搜索將 createdBy 字段保留為用戶?
這是我的架構
@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[];
}
createdBy 保存為用戶的唯一 ID(用戶集合的外鍵)我正在嘗試執行查詢以查找由具有特定 ID 的用戶創建的挑戰。 如果我將 createdBy 更改為 string(id) 它可以工作,但是我沒有獲得所有用戶屬性,nest 文檔也建議像我一樣創建它。 為了能夠在沒有任何編譯錯誤的情況下進行此查找,我應該進行哪些更改?
嘗試
this.challengesModel.find({ createdBy: userId as any })
這是因為 createdBy 不是字符串類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.