I'm currently using Objection.js with WebStorm to build a Rest API. But, when I want to make a relationQuery of Objection.js, my WebStorm shows me one out of two times that $query()
is unresolved.
I have installed the Objection.js TypeScript, but it doesn't work. I also tried to invalidate the WebStorm cache, but no better results.
Here are some screenshots of the errors
I also watched with Visual Studio Code, and the code in the first screenshot is well detected and solved
$query() resolved inside VSCode:
I use Node 12.17, WebStorm 2020.1 and Objection 2.1.3
Hoping you can help me.
Here is the complete code snipper of the error:
const Users = require('../../../database/Users');
const sendReset = require('../../../email/reset');
const { nanoid } = require('nanoid');
/**
* @api {post} /v1/users/reset ✔ Set a reset email for an account
* @apiName resetPassword
* @apiGroup Users
* @apiVersion 1.0.0
*
* @apiParam {String} email email address of the user
*
* @apiSuccessExample {json} Success-Response :
* HTML 200 Success
* {
* "text": "success"
* }
*/
async function resetPassword(req, res) {
let email = req.body["email"];
if (email) {
try {
let toEdit = await Users.query().findOne("email", email);
if (toEdit) {
let token = nanoid(42);
let edited = await toEdit.$query().patchAndFetch({'token': token});
if (edited)
await sendReset(edited);
}
res.status(200).json({"message": "success"});
} catch (e) {
res.status(500).json({"error": "Internal server error"});
}
}
}
module.exports = resetPassword;
Users.js:
/*
** Created by Frederic GOMEL on 03/03/2020 at 18:07
** Project: api
*/
const knex = require('../init/database');
const {Model} = require('objection');
Model.knex(knex);
class Users extends Model {
static get tableName() {
return 'users';
}
static get jsonSchema() {
return {
type: 'object',
required: ["email", "group_right", "firstName", "lastName", "token"],
properties: {
id: {type: 'integer'},
email: {type: 'string'},
group_right: {type: 'integer'},
firstName: {type: 'string'},
lastName: {type: 'string'},
password: {type: ['string', 'null']},
rfid_uid: {type: ['string', 'null']},
token: {type: ['string', 'null']}
}
};
}
static get relationMappings() {
const Racks = require('./Racks');
return {
racks: {
relation: Model.ManyToManyRelation,
modelClass: Racks,
join: {
from: 'users.id',
through: {
from: 'link_users_racks.user',
to: 'link_users_racks.rack'
},
to: 'racks.id'
}
}
};
}
}
module.exports = Users;
reset.js:
const mail = require('../init/mails');
const twig = require('twig');
function sendReset(user) {
return new Promise((resolve, reject) => {
twig.renderFile('./email/templates/resetPassword.twig', {
name: `${user["firstName"]} ${user["lastName"]}`,
support_email: global["config"]["support_email"],
url: `${process.env["APP_URL"]}/activate?email=${user["email"]}&token=${user["token"]}`,
title: `Demande de changement de mot de passe`,
favicon: `${process.env["API_URL"]}/assets/images/favicon.ico`,
logo: `${process.env["API_URL"]}/assets/images/logo.png`
}, async (err, html) => {
if (err) {
reject(err);
} else {
mail.sendMail({
sender: `JAD Technologie <${process.env["MAIL_MAIL"]}>`,
to: user['email'],
subject: `Demande de changement de mot de passe`,
html: html
}).then(() => {
resolve();
}).catch((err) => {
reject({"mail": true, err});
});
}
});
});
}
module.exports = sendReset;
I use Express.js 4.16.0