未处理的拒绝错误:整数的无效输入语法:“ true” Express +护照+ postgreSQL

时间:2018-11-08 20:00:05

标签: node.js postgresql express passport.js knex.js

有人可以告诉我到底发生了什么吗?我正在尝试制作一个简单的登录页面,我将knex与postreSQL结合使用,但遇到一个奇怪的错误,我无法理解:

Unhandled rejection error: invalid input syntax for integer: "true"
    at Connection.parseE (/home/idlesolution/Desktop/backend/user/user/node_modules/pg/lib/connection.js:554:11)
    at Connection.parseMessage (/home/idlesolution/Desktop/backend/user/user/node_modules/pg/lib/connection.js:379:19)
    at Socket.<anonymous> (/home/idlesolution/Desktop/backend/user/user/node_modules/pg/lib/connection.js:119:22)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

这是我的代码。首先,我执行发帖请求并使用护照进行身份验证:

    router.post('/login', passport.authenticate('local', {
    successRedirect: '/user/profile',
    failureRedirect: '/',

}))

然后在server.js文件中设置本地策略:

passport.use(new localStrategy((username, password, done) => {
    knex('users').select('password').where({username: username}).then((data) => {
        if(data.length === 0){
            return done(null, false)
        } else{

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

当我键入现有用户的信息时,错误就出现了,所以它在else块中。当我将true更改为随机字符串时,错误仍然存​​在,但是,如果将其更改为false则没有错误,但显然我不想那样处理

1 个答案:

答案 0 :(得分:0)

这是因为在找到用户后应该返回true的地方,您将返回user。然后,此user对象将用于serialize,并在随后的请求中,deserialize用户。另外,您应尝试获取id(以及密码)可用于序列化的用户。

因此您的代码应如下所示

passport.use(new localStrategy((username, password, done) => {
    knex('users').select('id', 'password').where({username: username}).then((data) => {
        if(data.length === 0){
            return done(null, false)
        } else {
            return done(null, data);
        }
    });
}));