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

222
Vistas
How can I save a specific property/one property in database using Adonis js or express js

How can I save a specific property/one property in database using Adonis js or express js I want to save only random number to database. How can I do this? Here is my code:

 public async updateProfile({request, response, auth}: HttpContextContract){
  const data = request.all() 
  const userAuth : any = auth.user?.toJSON()
  let user = await User.find(userAuth.id);
  if (!data.code){
  const random = Math.floor(Math.random()* (9999-1000+1))+1000
  //want to save random number here into database
  return response.status(403).ok({require2fa:true, message:"Two factor Authentication required"})
  }
  }
If code is comming from postman then this if block is not executed. and in else part which I did not include, do its job to update profile. But I want that when code is not comming from postman then only random number shoul be saved into db. Kindly Help me out. How can I do this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I solved this issue I explain the problem once again as @Imeerma commented that he did not understand the problem. Problem: I wanted to update the profile using AdonisJS. I applied a two-factor authentication code also, As I want that If the user does not send some 4-digit code from postman then random code is generated and saved into DB. But if a user sends some 4-digit code from postman then in else part I match the random code which is saved into DB to the code which the user sends. here is my code to update profile in Adonis JS:

 public async updateProfile({request, response, auth}: HttpContextContract){
        try{
            const data = request.all() 
            const userAuth : any = auth.user?.toJSON();
            let user = await User.find(userAuth.id);
            if (!data.code){
                const random = Math.floor(Math.random()* (9999-1000+1))+1000
                user?.merge({code:random})
                await user?.save()
                return response.status(403).ok({require2fa:true, message:"Two factor Authentication required"})
            }
            if (user){
                if (user?.code == data?.code){
                    user.merge(data);
                    await user.save();
                    return response.status(200).ok(user);
                }
                return response.status(403).ok("Invalid Authetication code")
            }
            return response.status(404).ok("User with this id is not found")
        }catch(error){
            console.log("server error!",error);
           return response.badRequest({error: {server: ['server error']}})
        }
    }

It runs correctly Now. Thanks @Imeerna for your cooperation

about 4 years ago · Juan Pablo Isaza Denunciar

0

The question is a bit unclear but assuming that you want to store something in a database when one of your API routes is called, you can simply execute that code where you put your comment. Please also see the comments I added.

public async updateProfile({request, response, auth}: HttpContextContract){
  const data = request.all() 
  const userAuth : any = auth.user?.toJSON()
  let user = await User.find(userAuth.id);

  if (!data.code){
    const random = Math.floor(Math.random()* (9999-1000+1))+1000
    
    //depending on your database implementation and assuming it's async something like this
    
    await db.save(random)

   //make sure to handle possible errors properly from your database call

    return response.status(403).ok({require2fa:true, message:"Two factor Authentication required"})
  }
  // you should also handle the else case, otherwise there is no return value
}
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