Nodejs和Hbase发送到客户端后无法设置标头

时间:2019-03-13 20:06:59

标签: node.js express hbase

我最近开始使用node.js,我想将其与Hbase集成。为此,我使用了node-hbase library。我用快递来满足我的要求。

我正在尝试建立用于登录用户的业务逻辑,但是似乎无法将响应发送回用户。我收到“将其发送到客户端后无法设置标题”的信息。我仔细检查了一下,发现我没有两次将响应发送回客户端。

下面是错误跟踪:

_http_outgoing.js:482
throw new ERR_HTTP_HEADERS_SENT('set');
^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:482:11)
at ServerResponse.header (/Users/user/Documents/video-server/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/Users/user/Documents/video-server/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/Users/user/Documents/video-server/node_modules/express/lib/response.js:267:15)
at ServerResponse.send (/Users/user/Documents/video-server/node_modules/express/lib/response.js:158:21)
at Row.hclient.table.row.get (/Users/user/Documents/video-server/index.js:104:15)
at client.connection.get (/Users/user/Documents/video-server/node_modules/hbase/lib/row.js:61:22)
at IncomingMessage.<anonymous> (/Users/user/Documents/video-server/node_modules/hbase/lib/connection.js:131:16)
at IncomingMessage.emit (events.js:189:13)
at Socket.socketCloseListener (_http_client.js:363:11)

可以在下面找到登录代码:

// Login
app.post('/login', (req, res) => {
    const email = req.body.email;
    const password = req.body.password;


    hclient.table('user').row(email).get('data', (error, value) => {
        if (value == null) {
            // Send to the login view
            return res.status(400).send({invalidLogin: true});
        } else {

            // Checking the password
            const hash = crypto.createHash('sha256').update(password).digest('hex');

            // Find the user password
            const userPass = value.find(function(element) {
                return element.column === "data:password";
            })["$"];

            // Check if the supplied password matches the user password
            if (hash !== userPass) {
                return res.status(400).send({invalidLogin: true})
            }

            // There is a match so we continue

            // Session setting
            // Setting the firstname
            req.session.firstname = value.find(function(element) {
                return element.column === "data:firstname";
            })["$"];

            // Setting the last name
            req.session.lastname = value.find(function(element) {
                return element.column === "data:lastname";
            })["$"];


            // Redirecting to the homepage
            return res.send({redirect: "/"});
        }
    });
}); 

1 个答案:

答案 0 :(得分:0)

该错误不是我自己而是位于node-hbase库中。回调方法被调用了两次。

升级到0.6.0版,一切运行顺利