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

166
Vistas
How can I display the username of a forum?

I want to find out how I can display the username of a user. For example user admin posts a forum, then I would see Created By: admin on the forum page, instead I can only fish out the ID.

I don't know a much about mongoose and I need someone who is familiar with it.

My Forum Model:

You see I have only ref: 'user' and this is grabbing the ObjectId("") from the User.

const forumSchema = ({
   forumName: {
      type: String,
      required: true,
   },
   forumDescription: {
      type: String,
      required: true,
   },
   user: { 
      type: Schema.Types.ObjectId,
      ref: 'user'
   },
   published_on: {
      type: String,
      default: moment().format("LLL")
   },
});

my userModel:

const UserSchema = mongoose.Schema({
   userID: {
      type: String,
      required: true,
   },
   userName: {
      type: String,
      required: true,
   },
   password: {
      type: String,
      required: true,
   },
   isAdministrator: {
      type: Boolean,
      deafult: false,
  },

});

Front-end :

As you can see only in {forum.user} there I can see the id from the user but I want his name not the id

 <footer className="blockquote-footer">
    Created by:{forum.user}
    Created on:{forum.published_on.substring(0,300)}
 </footer>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since you are using 'ref' to reference the user table. When you are fetching a forum document, use the populate() function in mongoose to get all the user details as a sub-object to the forum document.

example: forumShcema.find({_id:<forum_id>}).populate('user').exec()

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