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

165
Visualizações
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 Respostas
Responde à pergunta

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 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