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
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.
– Guillerbr