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

153
Vistas
I have to construct object inside axios promise but I can't because of await

So I'm fetching data from an api using axios. I'm then using that data to build an object that I want to eventually save to my mongodb collection.

I kept debugging and trying a console.log everything and reached an understanding of sorts.

app.get('/', async (req, res) => {
    let initialData = {};
    axios.get('http://localhost:3000/details').then((res) => {
        initialData = res.data;
      });
    const recruit = new RecruitModel({ email:initialData.email,
                                        mobile_number:initialData.mobile_number,
                                         name:initialData.name});
    try {
        await recruit.save()
        res.send("inserted data")
    } catch (error) {
        console.log(error)
    }
})

The issue is that I have to affect the values to recruit inside the promise because the data isn't accessible outside. If I put

app.get('/', async (req, res) => {
    let initialData = {};
    axios.get('http://localhost:3000/details').then((res) => {
        initialData = res.data;
    const recruit = new RecruitModel({ email:initialData.email,
                                        mobile_number:initialData.mobile_number,
                                         name:initialData.name});
      });

    try {
        await recruit.save()
        res.send("inserted data")
    } catch (error) {
        console.log(error)
    }
})

If I change it to this and console.log(recruit) the values actually are affected. but now recruit isn't accessible outside the promise.

If I add that try catch block like so,

app.get('/', async (req, res) => {
    let initialData = {};
    axios.get('http://localhost:3000/details').then((res) => {
        initialData = res.data;
        console.log(initialData);
        const recruit = new RecruitModel({ email:initialData.email,
                                            mobile_number:initialData.mobile_number,
                                             name:initialData.name});
            console.log(recruit);

            try {
            await recruit.save()
            res.send("inserted data")
            } catch (error) {
            console.log(error)
            }
      });
})

I get this error

SyntaxError: await is only valid in async functions and the top levelbodies of modules

about 4 years ago · Santiago Gelvez
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