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

94
Visualizações
The results of the map function in the frontend and backend are output differently

I got an array by executing the find function in mongoose. After that, I tried to extract only the values ​​of the data using map. But only undefined is returned.

I have created an example below. When I run the map function on the frontend and the backend, the results are different.

I don't know why the results are different. Also, is there a way to get the result of number 2 in the backend code?

example mongodb data

{name: 'aaa'},
{name: 'bbb'}

backend code

router.get("/getValue", async (req, res) => {
  try {
    const result = await Schema.find({}, "-_id");
    console.log(result); // original data
    let one = result.map((v) => v.name); // 1
    res.send({ success: true, result });
  } catch (error) {
    console.log(error);
    res.send({ success: false });
  }
});

frontend code

axios.get("/api/getValue").then((res) => {
      if (res.data.success) {
        const second = res.data.result.map((v) => v.name); //2
      }
    });

result

// original data
[{name: 'aaa'}, {name: 'bbb'}]

// 1
[undefined, undefined]

// 2
['aaa', 'bbb']
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There was a typo in the schema declaration. While editing the schema data, it was confirmed that the name of the key was entered incorrectly.

const Schema = new mongoose.Schema({
  username: {
    type: String,
  }
}

The name of the key in the previous db was name, but in the newly modified schema, only username could be searched, so the variable name could not be found.

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