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

Time:04-28

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?

CodePudding user response:

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
}

CodePudding user response:

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

  • Related