【发布时间】:2020-02-10 03:28:09
【问题描述】:
我将 ionic 与已实现 REST API 的节点服务器一起使用,但我遇到了一个问题:当我在前端执行多次更新查询时,服务器仅执行 6 个请求然后停止运行。
在 Ionic 应用程序(前端)中,我有这个:
update_loc() {
// Attempt to login in through our User service
this.busloc.update_loc(this.track_info).subscribe((resp) => {
console.log("Call provider in busloc.ts")
}, (err) => {
console.log("Updating location failed")
});
}
在我的提供商 (bus-loc.ts) 中,我有:
update_loc(track_info: any) {
let seq = this.api.post('update_loc', track_info).share();
seq.subscribe((res: any) => {
// If the API returned a successful response, locations has been updated
if (res.status == 'success') {
console.log("Location updated succesfully")
}
}, err => {
console.error('ERROR', err);
});
return seq;
}
在后端我有以下功能:
//Actualizar ubicacion
function updateLoc(req,res){
body = req.body;
TrackInfo.findOne({ where: { id: body.id } })
.then(tr_info=>{
if (!tr_info){
return res.status(404).send ('No track_info found');
}
tr_info.update({
is_active: body.is_active,
lat: body.lat,
lon: body.lon
})
console.log("Updated track info succesfully")
return res.status(200);
}).catch(err => {
return res.status(500).send ('Server Error in updateLoc');
});
}
现在我想知道如何完成 api 调用,因为我读过浏览器(在我的情况下是 Chrome)只能执行六个 http 请求,并且由于我连续多次调用 update_loc(),所以服务器被阻塞。 欢迎任何建议。感谢您的关注
【问题讨论】:
-
最好的方法可能是推迟每次调用,在我看来,您不应该在同一时间实验室中允许过多调用 API。谁或什么调用 update_loc() ?可能是这个问题,你有太多的使用这个功能。您是否考虑过实施套接字来更新您的应用程序?而不是每毫秒调用 api
-
Update_loc() 由 Observer 调用,因为例如我需要在数据库中每两秒更新一次纬度和经度,但我甚至在十秒后对其进行测试,并且服务器在六个请求后停止执行反正。这就是为什么我认为它必须与 api 调用有关。
-
您在后端使用什么技术?看起来您的后端没有关闭某些东西。来自 http 的 Angular 帖子不会保持打开状态。
-
你能提供一个stackblizt来试试你的代码吗?没有要测试的东西很难猜到。
-
我使用 Node.js 作为后端,通过 express 连接到 PostgresQL 数据库,我已经用后端代码更新了问题。你能看到任何错误吗?我不知道如何使用 stackblizt :c
标签: angular api http ionic-framework call