Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
CastError: la conversión a [indefinido] falló para el valor "[]" (escriba una cadena) en la ruta "comentarios.indefinido"

Soy bastante nuevo en nodo y mangosta. Estoy tratando de hacer un proyecto usándolos, pero me encuentro con un error al intentar completar. El comentario se guarda perfectamente en el esquema de comentarios, pero arroja un error cuando hago referencia al esquema de la organización. Por favor, avísenme sobre lo que estoy haciendo mal. Cualquier forma de ayuda será apreciada.

 // Post route for comment(on the Organization's profile page) router.post('/comment/:id', ensureAuthenticated,(req, res) =>{ let id = req.params.id; console.log(mongoose.Types.ObjectId.isValid(id)) const commentObject = new Comment({ sender: 'Fred kimani', commentBody: req.body.commentBody }) console.log(commentObject); commentObject.save((err, result) =>{ if(err){console.log(err)} else{ Organization.findByIdAndUpdate(id, {$push: {comments: result}}, {upsert: true}, (err, organization) =>{ if(err){console.log(err)} else{ console.log('======Comments====') } }) res.redirect('/users/organizationprofilepage/:id') } }) }); //Organization Schema const mongoose = require('mongoose'); const OrganizationSchema = new mongoose.Schema({ organization_name: { type: String, required: true }, email: { type: String, required: true }, password: { type: String, required: true }, date: { type: Date, default: Date.now }, category: { type: String, required: true }, isApproved: { type: Boolean, default: false }, image:{ type:String, required:true }, description: { type: String, required: true, }, comments: [{ type: mongoose.Types.ObjectId, ref: 'Comment' }], }, //{ typeKey: '$type' } ); OrganizationSchema.statics.getOrganizations = async function () { try { const organizations = await this.find(); return organizations; } catch (error) { throw error; } } //defines the layout of the db schema const Organization = mongoose.model('0rganization', OrganizationSchema); module.exports = Organization; //Comment schema const mongoose = require('mongoose'); const CommentSchema = mongoose.Schema({ sender: { type: String, }, commentBody: { type: String, required: false, }, date: { type: Date, default: Date.now }, }) CommentSchema.statics.getComments= async function () { try { const comments = await this.find(); return comments ; } catch (error) { throw error; } } const Comment= mongoose.model('Comment', CommentSchema); module.exports = Comment;

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  1. Intente cambiar el tipo de comments a mongoose.Schema.Types.ObjectId :
 comments: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Comment', }, ],
  1. Intente insertar el nuevo _id de elogio en el objeto Organization después de su creación, no en todo el objeto:
 commentObject.save((err, result) => { if (err) { console.log(err); } else { Organization.findByIdAndUpdate( id, { $push: { comments: result._id } }, // <- Change this line { upsert: true }, (err, organization) => { } ); ... } });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda