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

75
Visualizações
Filter by userId

I need to filter a list of posts and a list comments by userId. Am passing the userId params as postCreator and commentCreator. What am I doing wrong?

//data type for comments 
const commentSchema = new mongoose.Schema(
    {comment: {
      type: String,
      required: [true, "comment can not be empty"],
    },
    createdAt: {
      type: Date,
      default: Date.now(),
    },
    vote: {
      type: Number,
      default: 1,
    },
    commentCreator: {
      type: mongoose.Schema.ObjectId,
      ref: "User",
      required: [true, "comment must belong to a user."],
    },
    postCommentedOn: {
      type: mongoose.Schema.ObjectId,
      ref: "Post",
      required: [true, "a comment must belong to a post"],
    }, } )
 
 //data type for post is
    
    const postSchema = new mongoose.Schema(
  {
    postContent: {
      type: String,
      required: [true, "Post Content cannot be empty"],
    },
    postTitle: {
      type: String,
      required: [true, "Post Title cannot be empty"],
    },
    vote: {
      type: Number,
      default: 1,
    },
    createdAt: {
      type: Date,
      default: Date.now(),
    },
    postCreator: {
      type: mongoose.Schema.ObjectId,
      ref: "User",
      required: [true, "a post must belong to a user."],
    },
    image: {
      type: String,
    },
  },)

I need to filter a list of posts and a list comments by userId. Am passing the userId params as postCreator and commentCreator. What am I doing wrong?

let filter = {};
if (req.params.postId || req.params.userId)
  filter = {
    postCommentedOn: req.params.postId,
    postCreator: req.params.userId,
    commentCreator: req.params.userId,
  };
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I just asked your question to the OpenAI JavaScript Codex. It returned a result which I don't know if it really works or not. But I want to see if this actually works or not.

/* I need to filter a list of posts and a list comments by userId. Am passing the userId params as postCreator and commentCreator */
var posts = [
  {
    id: 1,
    title: 'Post 1',
    body: 'Post 1 body',
    userId: 1
  },
  {
    id: 2,
    title: 'Post 2',
    body: 'Post 2 body',
    userId: 2
  },
  {
    id: 3,
    title: 'Post 3',
    body: 'Post 3 body',
    userId: 1
  }
];
var comments = [
  {
    id: 1,
    body: 'Comment 1',
    userId: 1
  },
  {
    id: 2,
    body: 'Comment 2',
    userId: 2
  },
  {
    id: 3,
    body: 'Comment 3',
    userId: 1
  }
];
var postCreator = 1;
var commentCreator = 1;
var filteredPosts = posts.filter(function(post) {
  return post.userId === postCreator;
});
var filteredComments = comments.filter(function(comment) {
  return comment.userId === commentCreator;
});
console.log(filteredPosts);
console.log(filteredComments);

Please forgive me if this doesn't work. And if it does let me know. Thanks.

about 4 years ago · Santiago Trujillo Relatório

0

From the data type I can see that postCommentedOn, postCreator, commentCreator are of type ObjectID meanwhile the req.params are received in string format. So you need to cast them to ObjectId to use them in the filter.

Something like the following code should help.

 const mongoose = require('mongoose'); const ObjectId = mongoose.Types.ObjectId; filter = { postCommentedOn: ObjectId(req.params.postId), postCreator: ObjectId(req.params.userId), commentCreator: ObjectId(req.params.userId), }
about 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