Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

387
Visualizações
nestjs mongoose Type 'string' is not assignable to type 'Condition<LeanDocument<User>>

Im using nestjs with mongoose. I try to do a simple find query

 this.challengesModel.find({ createdBy: userId })

where this.challengesModel is injected like this

 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?

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. 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. What should I change in order to be able to do this find without any compliation errors?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try

 this.challengesModel.find({ createdBy: userId as any })

This is caused because createdBy isn't a string type.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda