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

88
Vistas
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 Respuestas
Responde la pregunta

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