【问题标题】:How to finish my http call to my REST API?如何完成对 REST API 的 http 调用?
【发布时间】: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


【解决方案1】:

您可以通过拨打unsubscribe取消最后一次通话

  subscription: Subscription;
  update_loc() {
    // cancel last call
    this.subscription.unsubscribe();
    // Attempt to login in through our User service
    this.subscription = this.busloc.update_loc(this.track_info).subscribe((resp) => {
      console.log("Call provider in busloc.ts")
    }, (err) => {
      console.log("Updating location failed")
    });
  }

您可以在这里找到更多信息:https://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http

【讨论】:

  • 观察者每 2 秒调用一次函数 update_loc(),然后我想执行 100 次或更多次。我使用 unsubscribe() 完成了 Observer,但是当 Observer 连续调用 update_loc() 超过六次时,它会停止执行更新查询。
  • 你无法绕过 chrome 到同一域的最大并行 http 连接数:browserscope.org/?category=network&v=top
  • 是的,这就是为什么我想知道如何关闭api调用,然后它不会超过http连接的最大数量,对吗?
  • 如果您使用取消订阅,最后一次通话将被关闭。让我给你解释一下,如果你在 api 调用未完成时再次调用 update_loc() 不需要知道结果,因为你会再次调用相同的 api,所以当你是 unsubscribe() 最后一次调用时,你 ara关闭最后一个 api 调用。有意义吗?
  • 我想你不明白 api 调用是如何工作的,当你使用 post 请求时,完成后会自动关闭。您的问题是您在短时间内对同一个 api 进行了太多调用,而 chrome 不允许这样做。
猜你喜欢
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
  • 2019-12-27
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 2012-07-31
相关资源
最近更新 更多