这是一个关于Nodejs(10.16)和express(4.16.4)中收益的新手问题,前端是本机0.59。这是成功查询后返回状态200的Node.js服务器上的代码:
@ResponseBody
@RequestMapping(value = "/trafficcameras", method=RequestMethod.GET)
public String parse(@RequestParam(value="ip_comm_status", required=false) String ip_comm_status_val, @RequestParam(value="camera_status", required=false) String camera_status_val)
{
return "Result is:";
}
上面的代码在react native中与以下代码一起调用:
router.post('/new', [auth_deviceid, auth_userinfo, auth_role(['admin', 'eventer', 'messager'])], async (req, res) => {
//prepare obj msg....
try {
await msg.save();
msg.user_name = req.body.alias;
msg.user_avatar = req.user.user_data.avatar;
const io = req.app.get("io");
const socket_id = req.body.socket_id;
const socket = io.sockets.connected[socket_id];
const room = msg.event_id.toString();
socket.join(room);
socket.to(room).emit("event message", msg);
console.log("after emit to room : ", room); //<<<==this line shown in console screen
return res.status(200); //<<<=== hang here with no error thrown. But instead res.status(200).send("I am ok") is working.
} catch (err) {
console.log("Error in saving/braodcasting a new message", err);
return res.status(400).send(err.message);
};
});
在控制台屏幕上,收到以下错误:
async _onSend(messages = []) {
//do something...
let obj = JSON.stringify({
_device_id: DeviceInfo.getUniqueID(),
sender_id: this.state.myself.id,
sender_grpmember_id:(this.props.grpmember_id || this.props.navigation.state.params.grpmember_id),
alias:this.props.alias,
socket_id: this.props.socket.id,
data: {
msg_body:messages[0].text,
upload_content_info: ""
},
event_id: this.props.navigation.state.params.eventId,
_grpmember_id:(this.props.grpmember_id || this.props.navigation.state.params.grpmember_id),
_group_id:(this.props.group_id || this.props.navigation.state.params.group_id),
});
try {
let url = `${GLOBAL.BASE_URL}/api/messages/new`;
console.log("Chat message URL : ", obj);
let res = await fetch(url, {
method: "POST",
headers: {
'Accept': 'application/json, text/plain',
'Content-Type': 'application/json',
"x-auth-token": result.password,
"x-auth-secret" : result.username,
},
body: obj,
});
}
catch (err) {
console.log("Error in saving message : ", err);
};
}
为什么09-25 17:19:20.325 15181 15252 I ReactNativeJS: 'Chat message URL : ', '{"_device_id":"0cfce7b7e86fa397","sender_id":1,"sender_grpmember_id":1,"alias":"jc","socket_id":"z-8JjwQvKo6ed57KAAAB","data":{"msg_body":"hello","upload_content_info":""},"event_id":1,"_grpmember_id":1,"_group_id":"1"}'
09-25 17:19:20.345 15181 15252 I ReactNativeJS: In GiftedChat render :
09-25 17:20:35.554 15181 15252 I ReactNativeJS: 'Error in saving message : ', { [TypeError: Network request failed]
09-25 17:20:35.554 15181 15252 I ReactNativeJS: line: 24115,
09-25 17:20:35.554 15181 15252 I ReactNativeJS: column: 31,
09-25 17:20:35.554 15181 15252 I ReactNativeJS: sourceURL: 'http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false' }
导致return res.status(200)
错误?
答案 0 :(得分:1)
您指出,您的请求大约需要15秒钟,return res.status(200);
仅设置响应的http状态代码,要完成请求,您可以使用res.json
,res.send
或{{1 }}。
在这种情况下,我认为您只需要发送http状态代码为200的成功
您可以使用res.end
(res.sendStatus(200);
是实现res.sendStatus
和res.status
的简写)而不是res.send
。