简体   繁体   English

为什么我的 Array Push 不能以正确的方式工作? Vue

[英]Why does my Array Pushes doesnt work the right way? Vue

a simple question: I want to add simple numbers to an array, which I want to compare later to another array.一个简单的问题:我想将简单的数字添加到一个数组中,稍后我想将其与另一个数组进行比较。 But I can't access the content of the update-Array.但我无法访问更新数组的内容。

This is my code:这是我的代码:

checkForUpdates(curr_dataset) { 
  var current = curr_dataset;
  var update = [];

  //put in array
  axios.get('http://localhost:3030/disruptions/', {
    params: {
      DisruptionCategory: 0
    }
  })
  .then(response => {
    console.log("push " + response.data.data.length)
    update.push(response.data.data.length);
  })
  .catch((error) => {
        console.log(error.data);
  });
  axios.get('http://localhost:3030/disruptions/', {
    params: {
      DisruptionCategory: 1
    }
  })
  .then(response => {
    console.log("push " + response.data.data.length)
    update.push(response.data.data.length);
  })
  .catch((error) => {
        console.log(error.data);
  });
  axios.get('http://localhost:3030/disruptions/', {
    params: {
      DisruptionCategory: 2
    }
  })
  .then(response => {
    console.log("push " + response.data.data.length)
    update.push(response.data.data.length);
  })
  .catch((error) => {
        console.log(error.data);
  });
  axios.get('http://localhost:3030/disruptions/', {
    params: {
      DisruptionCategory: 3
    }
  })
  .then(response => {
    console.log("push " + response.data.data.length)
    update.push(response.data.data.length);
  })
  .catch((error) => {
        console.log(error.data);
  });

  console.log(update[0]); //HERE I GET "undefined"

}

To continue and compare the content of my update-Array with the current-Array I need to be sure that I've got the right values...要继续并将我的更新数组的内容与当前数组进行比较,我需要确保我有正确的值......

Anyone an idea?任何人的想法?

This code is asynchronous.此代码是异步的。 I would recommend you take a look at how asynchronous javascript code works.我建议你看看异步 javascript 代码是如何工作的。

Basically what you are doing is this:基本上你在做什么是这样的:

  1. Creating an empty array创建一个空数组
  2. Make an Axios get request.发出 Axios 获取请求。 When this completes, go to 2.1, if it fails go to 2.2.完成后,go 到 2.1,如果它失败 go 到 2.2。
    • 2.1 Push to Array 2.1 推送到数组
    • 2.2 log error 2.2 日志错误
  3. Make an Axios get request.发出 Axios 获取请求。 When this completes, ..当这完成时,..
    • 3.1.. 3.1..
    • 3.2.. 3.2..
  4. .. ..
  5. Show me what the element at index 0 in the array.告诉我数组中索引 0 处的元素是什么。

You see, the calls 2.1/3.1/4.1 only get executed, WHEN the request returns successful.你看,调用 2.1/3.1/4.1 只有在请求返回成功时才被执行。 Javascript is not blocking the script until they complete. Javascript 在脚本完成之前不会阻止脚本。 So until it gets to 5., non of these requests should have completed or failed.因此,直到达到 5.,这些请求都不应该完成或失败。 That is why nothing gets pushed to the array.这就是为什么没有任何东西被推送到数组中。

Here on SO you will find many examples, blog entries and questions already relating to that.在 SO 上,您会发现许多与此相关的示例、博客条目和问题。

Furthermore, you should start using async/await for those use cases.此外,对于这些用例,您应该开始使用 async/await。 It is just way cleaner code and is easier to debug (in my opinion).它只是更简洁的代码并且更容易调试(在我看来)。 Also use const instead of var.也使用 const 代替 var。

an example would be:一个例子是:

async checkForUpdates(curr_dataset) {
  const current = curr_dataset
  const update = []
  const promises = [0,1,2,3].map(async i => {
    try {
      r = await axios.get('http://localhost:3030/disruptions/', { 
        params: {
          DisruptionCategory: i
        }
      })
      // execute the rest of the code here, like pushing to the array
    } catch (e) {
      console.log(e.data)
    }
  await Promise.all(promises)
  console.log(update[0])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM