mysqljs / sqlstring

Simple SQL escape and format for MySQL
MIT License
403 stars 78 forks source link

TypeError: val.toString is not a function #45

Closed mafsan786git closed 5 years ago

mafsan786git commented 5 years ago

When I'm trying to log in it is throwing me the error... here is error...

TypeError: val.toString is not a function
    at Object.escape (/home/mohd/Desktop/codebucket/loginsystem/node_modules/sqlstring/lib/SqlString.js:52:33)
    at Object.objectToValues (/home/mohd/Desktop/codebucket/loginsystem/node_modules/sqlstring/lib/SqlString.js:180:89)
    at Object.escape (/home/mohd/Desktop/codebucket/loginsystem/node_modules/sqlstring/lib/SqlString.js:54:26)
    at Object.format (/home/mohd/Desktop/codebucket/loginsystem/node_modules/sqlstring/lib/SqlString.js:100:19)
    at Connection.format (/home/mohd/Desktop/codebucket/loginsystem/node_modules/mysql/lib/Connection.js:274:20)
    at Connection.query (/home/mohd/Desktop/codebucket/loginsystem/node_modules/mysql/lib/Connection.js:192:22)
    at Strategy._verify (/home/mohd/Desktop/codebucket/loginsystem/config/passport.js:16:28)
    at Strategy.authenticate (/home/mohd/Desktop/codebucket/loginsystem/node_modules/passport-local/lib/strategy.js:88:12)
    at attempt (/home/mohd/Desktop/codebucket/loginsystem/node_modules/passport/lib/middleware/authenticate.js:361:16)
    at authenticate (/home/mohd/Desktop/codebucket/loginsystem/node_modules/passport/lib/middleware/authenticate.js:362:7)

AND MY LOG IN CODE IS GIVEN BELOWcan you please resolve this issue

const localStrategy = require('passport-local').Strategy;
const bcrypt = require('bcryptjs');
const mysql = require('mysql');

const connection = require('./database');

module.exports = function (passport) {
    passport.use(
        new localStrategy(
            {usernameField:'email',
            passwordField:'password',
            passReqToCallback:true
            },
            (email, password, done)=>{
                var sql = 'SELECT * FROM user WHERE email = ?';
                connection.query(sql,email,(error,results,fields)=>{
                    if(error) throw error;
                    //Match user
                    if(!results.length)
                        return done(null, false, {message:'That email is not register'});

                    //Match
                    bcrypt.compare(password,results[0].password,(err,isMatch)=>{
                        if(err) throw err;
                        if (isMatch) {
                            console.log(results[0]);
                            console.log('every thing is fine');

                            //return done(null,results[0]);
                        }else{
                            return done(null, false, {message:'Password incorrect'});
                        }
                    });
                });
            })
    );

    //serializeuser
    passport.serializeUser(function (user,done) {
        console.log(user.id);
        done(null,user.id);
    });

    //deserializeuser
    passport.deserializeUser(function (id,done) {
        connection.query('SELECT * FROM user WHERE id = ?',id,(err,results,fields)=>{
            done(err,results[0]);
        });
    });

};
dougwilson commented 5 years ago

Thanks for the report @mafsan786git . What steps do I need to take to be able to run that code above in order to reproduce the error?

mafsan786git commented 5 years ago

Thanks for the report @mafsan786git . What steps do I need to take to be able to run that code above in order to reproduce the error?

can you please see this repository https://github.com/mafsan786git/LoginSystem @dougwilson

dougwilson commented 5 years ago

Thank you. Anywhere I can find the steps to set up the app and then what to do to see the error in it?

mafsan786git commented 5 years ago

Thank you. Anywhere I can find the steps to set up the app and then what to do to see the error in it?

signup using localhost:3000/app/register Log in using localhost:3000/app/login

Thank you in advance

dougwilson commented 5 years ago

Because you have passReqToCallback:true at https://github.com/mafsan786git/LoginSystem/blob/8c6b04818760ce55014925b1d27655b0e50961b7/config/passport.js#L12 the first agument to your callback is the request, not the email. Just remove that configuration.

mafsan786git commented 5 years ago

Because you have passReqToCallback:true at https://github.com/mafsan786git/LoginSystem/blob/8c6b04818760ce55014925b1d27655b0e50961b7/config/passport.js#L12 the first agument to your callback is the request, not the email. Just remove that configuration.

Thank you for your support.