Можно ли использовать 1 функцию для извлечения объекта из конструктора с использованием JS ES6 Destructuring для доступа к различным столбцам в MySql db? - PullRequest
0 голосов
/ 09 мая 2020

Цель: сравнить вводимую мной электронную почту формы с электронными письмами в mysql таблице пользователей и, если нет, создать нового пользователя и хешированный пароль

Могу ли я удалить ссылку на столбец MySql из модели и определить параметр в Контроллер? Из модели

static findBy() {
    return db.execute('SELECT * FROM users WHERE users. = ?', []);

эти 2 работают:

static findById(id) {
return db.execute('SELECT * FROM users WHERE users.id = ?', [id]);}

   static findByEmail(email) {
   return db.execute('SELECT * FROM users WHERE users.email = ?', [email]);



module.exports = class User {
        constructor(id, firstname, surname, username, email, password, new_password_token, new_token_expiration) {
                this.id = id;
                this.firstname = firstname;
                this.surname = surname;
                this.username = username;
                this.email = email;
                this.password = password;
                this.new_password_token = new_password_token;
                this.new_token_expiration = new_token_expiration;

Из контроллера

exports.postSignup = (req, res, next) => {
    const email = req.body.email;
    const password = req.body.password;
    const confirmPassword = req.body.confirmPassword;
    User.findByEmail({
            email: email
        })
        .then(([email]) => {
                if (email) {
                    'error',
                    'E-Mail exists already, please pick a different one.'
                );
                return res.redirect('/signup');
            }
            return bcrypt
                .hash(password, 12)
                .then(hashedPassword => {
                    const user = new User({
                        email: email,
                        password: hashedPassword,
                        cart: {
                            items: []
                        }
                    });
                    return user.save();
                })
                .then(result => {
                    res.redirect('/login');
                })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...