How to update some fields of a table

Asked

Viewed 124 times

0

I am using the framework based on Java Scrit and Nodejs,Adonisjs.Need to update some fields in a table. Table fields user need to be changed, by default they come NULLS. The fields are passwordResetToken, passwordResetExpires

My controller:



async forgotPassword({ request, response }) {
    const sgMail = require("@sendgrid/mail");
    sgMail.setApiKey(process.env.SENDGRID_API_KEY);

    const { email } = request.body;

    try {
      const user = await User.find(email);    //.find({ email });
      const token = crypto.randomBytes(20).toString("hex");
      const now = new Date();
      now.setHours(now.getHours() + 1);                       //1 HR valid token

      // console.log(token)
      // console.log(now)

      await user.update({
        passwordResetToken: token,
        passwordResetExpires: now,
      });

      const msg = {
        to: email,
        from: env.SENDGRID_USE_EMAIL,                         //your e-mail register sendgrid
        subject: "Sending with Twilio SendGrid is Fun",
        text: "and easy to do anywhere, even with Node.js",
        html: token,
        email,
        //'<strong>and easy to do anywhere fd, even with Node.js {token} </strong> {token}',
      };

      sgMail.send(msg);

      //res.send({ Successfully: true, user: req.token });
      console.log(sgMail);
      console.log(token);
      console.log(now);
      //console.log(error);

      res.status(200).json({
        Success: "Request sent successfully,check token in your email!",
      });
    } catch (err) {
      console.log(err);
      response.status(400).send({ error: "E-mail does not exist!" });
      console.log(email);
    }
  }


The Error returned on console:


TypeError: user.update is not a function
    at UserController.forgotPassword (C:\SOFTWARE\singular_store\app\Controllers\Http\UserController.js:140:18)
    at async Server._routeHandler (C:\SOFTWARE\singular_store\node_modules\@adonisjs\framework\src\Server\index.js:121:25)
    at async AuthInit.handle (C:\SOFTWARE\singular_store\node_modules\@adonisjs\auth\src\Middleware\AuthInit.js:60:5)
    at async ConvertEmptyStringsToNull.handle (C:\SOFTWARE\singular_store\app\Middleware\ConvertEmptyStringsToNull.js:14:5)
    at async BodyParser.handle (C:\SOFTWARE\singular_store\node_modules\@adonisjs\bodyparser\src\BodyParser\index.js:284:7)

I tried several times and failed, even though the error was on the face and well described by Adonis.I tried some unsuccessful alternatives. I believe that the error may be simple, of syntax or something that I still can’t see. Thanks in advance to those who can help me!

Yours sincerely! Guilherme Henrique

2 answers

1

What may be occurring is that the variable "user" has the content returned by " await User.find(email);"

const user = await User.find(email);  

If the user is not located, the "user" will not update method. This justifies the error.

One way to avoid error message would be to check if "user" has content before doing the update.

if (user){ //Aqui foi feita a alteracao
  await user.update({
    passwordResetToken: token,
    passwordResetExpires: now,
  });

The code would look like this:

try {
      const user = await User.find(email);    //.find({ email });
      const token = crypto.randomBytes(20).toString("hex");
      const now = new Date();
      now.setHours(now.getHours() + 1);                       //1 HR valid token

      // console.log(token)
      // console.log(now)


      if (user){ //Aqui foi feita a alteracao
      await user.update({
        passwordResetToken: token,
        passwordResetExpires: now,
      });
}

      const msg = {
        to: email,
        from: env.SENDGRID_USE_EMAIL,                         //your e-mail register sendgrid
        subject: "Sending with Twilio SendGrid is Fun",
        text: "and easy to do anywhere, even with Node.js",
        html: token,
        email,
        //'<strong>and easy to do anywhere fd, even with Node.js {token} </strong> {token}',
      };

      sgMail.send(msg);

      //res.send({ Successfully: true, user: req.token });
      console.log(sgMail);
      console.log(token);
      console.log(now);
      //console.log(error);

      res.status(200).json({
        Success: "Request sent successfully,check token in your email!",
      });
    } catch (err) {
      console.log(err);
      response.status(400).send({ error: "E-mail does not exist!" });
      console.log(email);
    }
  • Thanks for the @Bins reply, I’ve been having problems in the meantime. But I will go back to work on this project and I will check your answer today.

1





    const { email } = request.body;
    try {
      //const { email } = request.body;
      const user = await User.findBy("email", email);
      const token = crypto.randomBytes(15).toString("hex");
      const now = new Date();
      now.setHours(now.getHours() + 1); //1 HR valid token


      if (user) {
        user.merge({
          passwordResetToken: token,
          passwordResetExpires: now,
        });

        //SAVE DATAS
        await user.save();
      }

      response.status(200).json({
        Success: "Request sent successfully,check token in your email!",
      });

    
    } catch (err) {
      console.log(err);
      response.status(400).send({ error: "Fatal error try again" });
      console.log(err);
    }
  }



Browser other questions tagged

You are not signed in. Login or sign up in order to post.