"发送后无法设置标头。"

时间:2017-07-30 18:53:43

标签: json node.js express

  

发送后无法设置标头。

我确保我只调用res.send()一次(或在我的情况下res.json())(Can't set headers after they are sent node js)(NodeJs Can't set headers after they are sent)。然后我继续和我的res.json()一起打电话回来,以便之后不能再打电话。

app.get('/route/:rID/eta', r_cach.route(1), function(req, res) {
    routes.eta(req.params.rID, req.query.init).then(r => {
        return res.status(200).json({ success: true, data: r});
    }).catch(error => {
        console.error(error);
        return res.status(500).json({ success: false, data: error });
    });
});

我在第三个底线(return res.status(500).json({ success: false, data: error });)上收到错误。我做错了什么?

我没有r_cach以外的任何其他中间件Express Redis Cache。除了r_cach之外,我使用以下代码添加了一些额外的标头:

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

0 个答案:

没有答案
相关问题