TypeError:无法读取属性'用户名'在Node JS中未定义

时间:2018-01-08 04:41:39

标签: javascript node.js node-mysql

我在邮递员中尝试发帖登录时出错...但显示的错误无法读取属性用户名,这是我的代码

这是控制器

var connection = require('../../config/db');

function Todo() {
    this.create = function (req, res, next) {

        var username = req.body.username;
        var password = req.body.password;

        connection.acquire(function (err, con) {
            con.query('SELECT s.id, s.username, s.sls_nama, s.password, g.id as id_group, g.title FROM salesmen s, groups g WHERE g.id = s.group_id AND username = ?', [username], function (error, results, fields) {
                if (error) throw error;
                if (results.length == 0) {
                    // connected!;  
                    if (password == results[0].password) {
                        results[0]['status'] = "Success"
                        res.json(results[0]);
                        res.status(200).send();
                    } else {
                        results[0]['status'] = "Wrong Password"
                        res.json(results[0]);
                        res.status(200).send();
                    }
                } else {
                    res.status(401).json({
                        message: "Wrong password or username"
                    });
                }
            });
        });
    }
}

module.exports = new Todo();    

这是路由器

var login = require('../controllers/login');

module.exports = {
    configure: function(app) {
        app.route('/login').post(login.create);
    }
};

this is the error shown in postman

1 个答案:

答案 0 :(得分:0)

在你的where子句中,由于你为表添加了别名,你必须有s.username =?而不是用户名=?

SELECT s.id, s.username, s.sls_nama, s.password, g.id as id_group, g.title FROM salesmen s, groups g WHERE g.id = s.group_id AND s.username = ?