Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
Filtrar por ID de usuario

Necesito filtrar una lista de publicaciones y una lista de comentarios por ID de usuario. Estoy pasando los parámetros de ID de usuario como postCreator y commentCreator. ¿Qué estoy haciendo mal?

 //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, }, },)

Necesito filtrar una lista de publicaciones y una lista de comentarios por ID de usuario. Estoy pasando los parámetros de ID de usuario como postCreator y commentCreator. ¿Qué estoy haciendo mal?

 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 answers
Answer question

0

Acabo de hacer su pregunta al Codex JavaScript de OpenAI. Devolvió un resultado que no sé si realmente funciona o no. Pero quiero ver si esto realmente funciona o no.

 /* 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);

Por favor, perdóname si esto no funciona. Y si me avisas. Gracias.

about 4 years ago · Santiago Trujillo Report

0

Desde el tipo de datos, puedo ver que postCommentedOn, postCreator, commentCreator son del tipo ObjectID mientras tanto, los req.params se reciben en formato de cadena. Por lo tanto, debe convertirlos a ObjectId para usarlos en el filtro.

Algo como el siguiente código debería ayudar.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!