续集& passport:TypeError:val.replace不是函数

时间:2017-07-14 08:58:24

标签: javascript mysql node.js passport.js passport-local

我不断收到一个非常奇怪的错误。 我试图解决它,但我失败了。 在错误中,查询也没有正确记录。我真的不知道这里出了什么问题。 我的passport.js策略:

var LocalStrategy = require('passport-local').Strategy;
var sequelize = require('sequelize');
var sequelize = new sequelize('database', 'user', 'password');


var mysql = require('mysql');

var connection = mysql.createConnection({
  database: 'kitsune',
    host: 'localhost',
    user: 'root',
    password: 'root'
});

connection.query('USE kitsune');


module.exports = function(passport) {

    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });

    passport.deserializeUser(function(id, done) {
        connection.query("select * from penguins where id = " + id, function(err, rows) {
            done(err, rows[0]);
        });
    });
        passport.deserializeUser(function(id, done) {
        connection.query("select * from penguins where username = " + username, function(err, rows) {
            done(err, rows[0]);
        });
    })

    passport.use('local-signup', new LocalStrategy({
        penguinUsername: 'username',
        emailField: 'email',
        passwordField: 'password',
        passReqToCallback: true
    },

    function(req, username, email, password, done) {

        connection.query("select * from penguins where email = '" + email + "'", function(err, rows) {
            connection.query("select * from penguins where username = '" + username + "'", function(err, rows) {
            console.log(rows);
            console.log("above row object");
            if (err) return done(err);
            if (rows.length) {
                return done(null, false, req.flash('signupMessage', 'That email / username is already taken.'));
            } else {

                var newUserMysql = new Object();
                newUserMysql.username = username;
                newUserMysql.email = email;
                newUserMysql.password = password;

                sequelize.query('INSERT INTO penguins (username, email, password) VALUES (?, ?, MD5(?))',
  { replacements: [username, email, password], type: sequelize.QueryTypes.INSERT }
).then(rows => {
                console.log(sequelize.query);
                connection.query(sequelize.query, function(err, rows) {
                    newUserMysql.id = rows.insertId;

                    return done(null, newUserMysql);
                    })
                });
            }

        });
    });
    }));
    passport.use('local-login', new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password',
        passReqToCallback: true
    },

    function(req, email, password, done) {

        connection.query("SELECT * FROM `penguins` WHERE `email` = '" + email + "'", function(err, rows) {
            if (err) return done(err);
            if (!rows.length) {
                return done(null, false, req.flash('loginMessage', 'No user found.'));
            }

            if (!(rows[0].password == password)) return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));

            return done(null, rows[0]);

        });



    }));

};

产生错误的代码片段:

var newUserMysql = new Object();
                newUserMysql.username = username;
                newUserMysql.email = email;
                newUserMysql.password = password;

                sequelize.query('INSERT INTO penguins (username, email, password) VALUES (?, ?, MD5(?))',
  { replacements: [username, email, password], type: sequelize.QueryTypes.INSERT }
).then(rows => {
                console.log(sequelize.query);
                connection.query(sequelize.query, function(err, rows) {
                    newUserMysql.id = rows.insertId;

                    return done(null, newUserMysql);
                    })
                });
            }

        });
    });
}));

错误如下:

C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master>node server.js
Site is live
Nice! Database looks fine
[]
above row object
C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\protocol\Parser.js:79
        throw err; // Rethrow non-MySQL errors
        ^

TypeError: val.replace is not a function
    at Object.SqlString.escape (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\sequelize\lib\sql-string.js:63:15)
    at C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\sequelize\lib\sql-string.js:86:22
    at String.replace (<anonymous>)
    at Object.SqlString.format (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\sequelize\lib\sql-string.js:81:14)
    at Object.format (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\sequelize\lib\utils.js:84:22)
    at Sequelize.query (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\sequelize\lib\sequelize.js:792:19)
    at Query._callback (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\app\config\passport\passport.js:71:27)
    at Query.Sequence.end (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24)
    at Query._handleFinalResultPacket (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\protocol\sequences\Query.js:137:8)
    at Query.EofPacket (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\protocol\sequences\Query.js:121:8)
    at Protocol._parsePacket (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\protocol\Protocol.js:280:23)
    at Parser.write (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\protocol\Parser.js:75:12)
    at Protocol.write (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master\node_modules\mysql\lib\Connection.js:103:28)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)

C:\Users\Daan\Downloads\using-passport-with-sequelize-and-mysql-master\using-passport-with-sequelize-and-mysql-master>

0 个答案:

没有答案