React Native / Node API:在发送之后无法设置标题

我遇到了一个与我的Node API交互的原生应用程序的问题。 当我尝试发布数据通过节点更新数据库中的某些值(然后转到存储过程)时,我得到一个500 – 在Node 中发送后,无法设置标题

我能find的所有东西都表示这可能是由于发送了两次响应。 我不认为这是这种情况。 我已经在邮递员testing,事情工作正常,它返回正确的返回数据200的状态。

我正在尝试将数据发布到API,如下所示:

const myHeaders = new Headers(); myHeaders.append('Content-Type', 'application/json'); fetch(`http://localhost:3000/user/preferences`, { method: 'POST', headers: myHeaders, mode: 'cors', cache: 'default', body: JSON.stringify({ id: '1', minage: this.state.minageValue, maxage: this.state.maxageValue }) }) .then(response => response.json()) .then(body => console.log(body)) .catch(err => console.log(err)); 

我在API端接收数据,并将数据传递给存储过程:

 function updatePreferences(req, res, next) { console.log(req); var userid = req.body[0].id; var minage = req.body[0].minage; var maxage = req.body[0].maxage; db.func('___spsavepreferences', [userid, minage, maxage]) .then(function(data){ res.status(200) .json({ status: 'success', preferences: data }); }); } 

有任何想法吗?

编辑:

不知道这是否告诉我什么,但我已经logging错误消息到控制台:

 Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:504:11) at ServerResponse.setHeader (_http_outgoing.js:511:3) at ServerResponse.header (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:730:10) POST /user/preferences 500 410.809 ms - 189 at ServerResponse.send (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:170:12) at done (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:967:10) at Object.exports.renderFile (/Users/user/Projects/Project_node/node_modules/jade/lib/index.js:374:12) at View.exports.__express [as engine] (/Users/user/Projects/Project_node/node_modules/jade/lib/index.js:417:11) at View.render (/Users/user/Projects/Project_node/node_modules/express/lib/view.js:128:8) at tryRender (/Users/user/Projects/Project_node/node_modules/express/lib/application.js:640:10) at Function.render (/Users/user/Projects/Project_node/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/Users/user/Projects/Project_node/node_modules/express/lib/response.js:971:7) at /Users/user/Projects/Project_node/app.js:51:7 at Layer.handle_error (/Users/user/Projects/Project_node/node_modules/express/lib/router/layer.js:71:5) at trim_prefix (/Users/user/Projects/Project_node/node_modules/express/lib/router/index.js:315:13) at /Users/user/Projects/Project_node/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/Users/user/Projects/Project_node/node_modules/express/lib/router/index.js:335:12) 

编辑2:明白了。 我需要接受req.body.id,req.body.minage等值…而不是req.body [0] .id。 奇怪得到这样的错误,但解决了错误。

如果您在邮递员中看到不同的行为,那是因为邮递员只是直接执行POST ,而浏览器在尝试POST之前却做了CORS预检选项请求 。

所以这听起来像是导致“发送后无法设置标头”的请求,你看到的是OPTIONS请求,而不是POST请求。

您可以通过发送邮递员的OPTIONS请求来确认,并确保也提供一个Origin请求标头作为请求的一部分。 curl你可以这样做:

 curl -X OPTIONS -i -H 'Origin: http://xy' http://localhost:3000/user/preferences 

这可能会产生您从浏览器请求中看到的相同的500错误。

至于如何解决这个问题,我认为没有其他人可以告诉你没有看到更多的服务器端代码。 但是看起来似乎是,在你的代码的某个地方,你有一个或多个处理OPTIONS请求的逻辑的地方,而在OPTIONS某个地方 – 处理代码,它正试图在已经发送之后发送标题。

为了追踪到哪里,看起来好像要查看服务器端的服务器日志,并查找发生500故障时logging的任何消息。

顺便说一下,在这种情况下,触发浏览器执行COR预检OPTIONS请求的myHeaders.append('Content-Type', 'application/json')myHeaders.append('Content-Type', 'application/json')部分。