English 中文(简体)
包含未经界定的,尽管从职能中分配了一定价值
原标题:JS variable containing undefined despite it being assigned a defined value from a function

该守则意在从一个地方网数据库中获取所有文件,然后建立一个有所有问题的阵列,然后产生随机问题。 取自这一阵列。 支持者正在工作,所设定的职能——问题正在正确获得价值观。 The console. 职能内的记录也表明这一职能正在发挥作用,因为我的确在职能的确是专人的情况下回避了界定的问题。 但是,在更新职能中,没有界定。

我预计,这种补贴将在更新职能范围内确定,但不是。 简言之,问题 在规定的范围内,如果执行 con(问题)的话,问题功能将恢复确定的价值。 在更新职能范围内安装了ole(id)时,它没有界定。 我认为,这或许与我在两个职能面前所做的那样做,但我迄今未能看到这一点。 定购单(问题)和更新的长度也一样。

如果任何人知道发生这种情况的原因,我将非常赞赏解释。 感谢您的时间!

APILINK = // link to the backend
update(APILINK);

async function set_problem(url){
  let p_arr = []
  url = url + "action/getting_problems";
  fetch(url).then(res => res.json())
  .then(function(data) {
    data.forEach(problem => {
      p_arr.push(problem._problemId);
      console.log(p_arr);
    });
  }).then(()=> {
    let problemId = p_arr[Math.floor(Math.random() * p_arr.length)]
    console.log(problemId);
    return problemId;
  });
}


async function update(url){
  let id = await set_problem(url);
  url = url + id;
  console.log(url);
  console.log("id = " + id)
  // More code that is unrelated to the problem
最佳回答

您没有在<代码>_problem内发表任何返回声明,因此暗中返回未界定(然后用<代码>sync的许诺加以总结)。 <代码>.then的回程说明,tback don t causes set_problem<>/code>,以退还任何款项。

我建议你不要与时俱进。 • 编写贵国法典,编号为:

async function set_problem(url) {
  let p_arr = [];
  url = url + "action/getting_problems";
  const res = await fetch(url);
  const data = await res.json();
  data.forEach((problem) => {
    p_arr.push(problem._problemId);
    console.log(p_arr);
  });
  let problemId = p_arr[Math.floor(Math.random() * p_arr.length)];
  console.log(problemId);
  return problemId;
}

如果你倾向于使用<代码>.then,则至少需要添加一份返回声明,以兑现你创造的许诺:

function set_problem(url) {
  let p_arr = [];
  url = url + "action/getting_problems";
  return fetch(url) // <---- added
    .then((res) => res.json())
    .then(function (data) {
      data.forEach((problem) => {
        p_arr.push(problem._problemId);
        console.log(p_arr);
      });
    })
    .then(() => {
      let problemId = p_arr[Math.floor(Math.random() * p_arr.length)];
      console.log(problemId);
      return problemId;
    });
}

自第二个<代码>以来,还不需要第三个<代码>。 然后,是没有做任何同步做的事的。

function set_problem(url) {
  url = url + "action/getting_problems";
  return fetch(url)
    .then((res) => res.json())
    .then(function (data) {
      let p_arr = [];
      data.forEach((problem) => {
        p_arr.push(problem._problemId);
        console.log(p_arr);
      });
      let problemId = p_arr[Math.floor(Math.random() * p_arr.length)];
      console.log(problemId);
      return problemId;
    });
}
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签