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

105
Vistas
Array of {key: value} in mongoose schema

I have a comments field in my interface and schema as follows

export interface Blog extends Document {
    ...
    comments: { user: ObjectId; comment: string }[]
    ...
}


const blogSchema = new Schema<Blog>({
    ...
    comments: [
        {
            user: {
                type: ObjectId
            },
            comment: {
                type: String
            }
        }
    ],
    ...
})

However, schema throws this error 'ObjectId' only refers to a type but is being used as a value here.. I know that schema is written kind of in a weird way, but I'm confused with how to improve.

What I want in database is something like

[
    {
        user: "Vivid",
        comment: "Vivid's comment"
    },
    {
        user: "David",
        comment: "David's comment"
    }
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe you need to change ObjectId to

mongoose.Schema.Types.ObjectId

Either that, or you could do something like:

const mongoose = require('mongoose');
// ... other imports
const { ObjectId } = mongoose.Schema.Types;
// ... now use in code as `ObjectId`
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