【发布时间】:2019-09-09 22:16:36
【问题描述】:
我有一个提交患者姓名的帖子请求,服务器应该回复我patient_id 作为响应。我收到了 200 条回复给客户,但是我没有收到我需要的 patient_id 回复。当我在服务器上进行控制台登录时,我可以看到 patient.id 已生成并且也没有错误。想知道我是否缺少什么?
Response -
body: (...), bodyUsed: false, headers: Headers {}, ok: true, redirected: false, status: 200, statusText: "OK", type: "basic", url: "http://localhost:4000/patient/add"
//client side post
handleSubmit(e) {
e.preventDefault();
const postUrl = '/patient/add';
fetch(postUrl, {
method: 'POST',
headers: {'Content-Type': 'text/plain'},
body: this.state.patientName
})
.then(response=> {
if (!response.ok) console.log('failed', response);
else console.log(response);
});
}
this.app.post('/patient/add', bodyParser.text(),
this.route_post_patient_add.bind(this));
async route_post_patient_add(req, res) {
/** @type {string} */
const body = req.body;
if (body === undefined) {
logger.warning('Set room patient failed, body missing');
res.sendStatus(400);
return;
}
if (body === "") {
logger.warning(' body is empty');
res.sendStatus(400);
return;
}
try {
const patient_id = await this.save_patient(body);
res.send(patient_id);
console.log(patient_id); //logs the id that is generated
}
catch (err) {
logger.error('Set patient failed, internal error', { err });
res.sendStatus(500);
}
}
【问题讨论】:
-
在您的浏览器“网络”开发者工具中实际的响应正文是什么样的?
标签: javascript node.js api express server