模型文件node.js中没有控制台日志

时间:2018-06-05 02:56:06

标签: node.js express console.log

我正在尝试调试我的isPasswordValid文件中AuthenticationController变量为假的原因,所以我决定在我的用户模型中设置控制台日志,除非它们没有显示在我的终端中。< / p>

为什么?

User.js

const Promise = require('bluebird');
const bcrypt = Promise.promisifyAll(require('bcrypt-nodejs'));

function hashPassword(user, options) {
    const SALT_FACTOR = 8;

    console.log('hash');
    if(!user.changed('password')) {
        return;
    }

    return bcrypt
        .genSaltAsync(SALT_FACTOR)
        .then(salt => bcrypt.hashAsync(user.password, salt, null))
        .then(hash => {
            user.setDataValue('password', hash);
        })
}

module.exports = (sequelize, DataTypes) => {
    try {
        console.log('inside user');
        const User = sequelize.define('User', {
            email: {
                type: DataTypes.STRING,
                unique: true
            },
            password: DataTypes.STRING
        }, {
            hooks: {
                beforeCreate: hashPassword,
                beforeUpdate: hashPassword,
                beforeSave: hashPassword
            }
        });


        User.prototype.comparePassword = function (password) {
            return bcrypt.compareAsync(password, this.password);
        };
        return User;

    } catch(err) {
        console.log('error occured in user model: ' + err)
    }


};

AuthenticationController.js

const { User } = require('../models');
const jwt = require('jsonwebtoken');
const config = require('../config/config');

function jwtSignUser (user) {
    const ONE_WEEK = 60 * 60 * 24 * 7;
    return jwt.sign(user, config.authentication.jwtSecret, {
        expiresIn: ONE_WEEK
    })
}

module.exports = {

    async register(req, res) {
        try {
            const user = await User.create(req.body);

            const userJson = user.toJSON();
            res.send({
                user: userJson,
                token: jwtSignUser(userJson)
            });
        } catch (err) {
            res.status(400).send({
                error: 'This email account is already in use.'
            });
        }
    },

    async login(req, res) {
        console.log('here');

        try {
            const {email, password} = req.body;
            const user = await User.findOne({
                where: {
                    email: email
                }
            });

            if(!user) {
               res.status(403).send({
                   error: 'The username was incorrect'
               })
            }

            console.log('before comparePass');
            const isPasswordValid = await user.comparePassword(password);
            console.log('isPasswordValid : ' + isPasswordValid);

            if(!isPasswordValid) {
                res.status(403).send({
                    error: 'The password was incorrect'
                })
            }
            const userJson = user.toJSON();
            res.send({
                user: userJson,
                token: jwtSignUser(userJson)
            });
        } catch (err) {
            res.status(500).send({
                error: 'An error has occured trying to occur'
            });
        }
    }
}

0 个答案:

没有答案