Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs-can';在其他请求之后,我们不会响应_Node.js_Promise_Request - Fatal编程技术网

Node.js nodejs-can';在其他请求之后,我们不会响应

Node.js nodejs-can';在其他请求之后,我们不会响应,node.js,promise,request,Node.js,Promise,Request,我需要在请求后向API发送响应,但这不起作用: app.post('/auth/login', (req, res, next) => { if (req.body.login && req.body.password) { var options = { method: 'POST', url: 'https://auth.etna-alternance.net/login', resolveWithFul

我需要在请求后向API发送响应,但这不起作用:

    app.post('/auth/login', (req, res, next) => {
    if (req.body.login && req.body.password) {

        var options = { method: 'POST',
        url: 'https://auth.etna-alternance.net/login',
        resolveWithFullResponse: true,
        headers:
        {'cache-control': 'no-cache',
        'content-type': 'multipart/form-data;' },
        formData: { login: req.body.login, password: req.body.password } };
        request(options)
        .then(function (response) {
            console.log(response.body);
            var rawcookies = response.headers['set-cookie'];
            console.log(rawcookies);
            res.sendStatus(200);
        })
        .catch(function (err) {
             res.sendStatus(300);
        });

    } else {
    //  res.sendStatus(400);
    }
    return next();

});
我的错误:

Unhandled rejection Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)
at ServerResponse.setHeader (_http_outgoing.js:501:3)
at ServerResponse.header (/Users/nathan/API-Poulette/node_modules/express/lib/response.js:767:10)
at ServerResponse.contentType (/Users/nathan/API-Poulette/node_modules/express/lib/response.js:595:15)
at ServerResponse.sendStatus (/Users/nathan/API-Poulette/node_modules/express/lib/response.js:357:8)
at /Users/nathan/API-Poulette/src/auth.js:41:16
at tryCatcher (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/promise.js:689:18)
at Async._drainQueue (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/Users/nathan/API-Poulette/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
我使用,我真的需要等待并在我自己的API上发送API响应


感谢您的帮助

您正在异步处理程序中调用
res.sendStatus(200)
。您还正在调用
next()
。结果是在处理程序返回之前调用
next()
。其效果是,在该路由之后链中的任何中间件(可能只是您的错误处理程序)都在
请求(选项)之前响应请求。然后()
返回。当您收到
request()
的回复时,您已经发送了标题。快速的解决方法是去掉
next()
,因为在这之后不应该有更多的中间件。您需要响应请求或调用
next()
,但不能同时调用两者。

您正在异步处理程序中调用
res.sendStatus(200)
。您还正在调用
next()
。结果是在处理程序返回之前调用
next()
。其效果是,在该路由之后链中的任何中间件(可能只是您的错误处理程序)都在
请求(选项)之前响应请求。然后()
返回。当您收到
request()
的回复时,您已经发送了标题。快速的解决方法是去掉
next()
,因为在这之后不应该有更多的中间件。您需要响应请求或调用
next()
,但不能同时调用两者。

您正在if/else范围外调用next


请求是异步的,因此当您调用它时,它会启动请求,然后传递到下一个操作,即next(),直到它得到响应。

您在if/else范围外调用next


请求是异步的,因此当您调用它时,它会启动请求,然后传递到下一个操作,即next(),直到它得到响应。

基本上意味着您已经处于Body或Finished状态,但某些函数试图设置头或状态码。因为你已经在代码中的其他地方做了,这可能会有所帮助:是的,但我在读之前不调用res,但我在第一次之前不使用res.send或任何东西,简单地说,是的,你可以
res.sendStatus
多次出现在您的代码中!!到目前为止你试过什么?尝试调试,然后可能会发布更多的代码。因为没有更多的细节,这里有两个类似的问题,所以看看它们基本上意味着你已经处于Body或Finished状态,但是一些函数试图设置一个header或statusCode。因为你已经在代码中的其他地方做了,这可能会有所帮助:是的,但我在读之前不调用res,但我在第一次之前不使用res.send或任何东西,简单地说,是的,你可以
res.sendStatus
多次出现在您的代码中!!到目前为止你试过什么?尝试调试,然后可能会发布更多的代码。因为没有更多的细节,有两个类似的问题,所以看看他们的工作!realy reactive=)谢谢!realy reactive=)