最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

为什么我没有成功地在第二个代码部分中使用嵌入式“if”来完成它的工作?

IT培训 admin 6浏览 0评论

为什么我没有成功地在第二个代码部分中使用嵌入式“if”来完成它的工作?

var checking_location = "none"

const getentitiesByType = (arr, type) => {
  for (let i in arr) {
    if (arr[i].type === type) {


      checking_location = "exists"
      return arr[i].entity
    }
  }
  return null;
}
if (!meeting.location) {
  if (checking_location != 'exists') {
    rl.question('where is the location ', function(answer) {
      // session.send("The location you gave:" answer);
      rl.close();
      session.send(answer)
        // console.log(tryagain(answer, 'Calendar.Location'));
      session.send(tryagain(answer, 'Calendar.Location'));
    });
  }
} else {
  next();
}

我想在这里做的是在if (!meeting.location)中有一个循环,如果checking_location保持等于none。基本上我想检查某个Json字段是否存在,如果它不存在我想继续在rl.question中提问。我的问题是代码只在第一次工作,那么即使我给另一个输入不包含必填字段我没有得到那个问题。请注意,这不是整个代码,但它足以理解我的实现中可能存在的问题点。

回答如下:

需要在某处调用getentitiesByType,只需将其分配给变量就不会使函数运行:getentitiesByType(youArr, yourType)

另外,作为旁注,不要使用checking_location的字符串值,只需重命名变量并使用布尔值。例如:var hasLocation = false

为什么我没有成功地在第二个代码部分中使用嵌入式“if”来完成它的工作?

var checking_location = "none"

const getentitiesByType = (arr, type) => {
  for (let i in arr) {
    if (arr[i].type === type) {


      checking_location = "exists"
      return arr[i].entity
    }
  }
  return null;
}
if (!meeting.location) {
  if (checking_location != 'exists') {
    rl.question('where is the location ', function(answer) {
      // session.send("The location you gave:" answer);
      rl.close();
      session.send(answer)
        // console.log(tryagain(answer, 'Calendar.Location'));
      session.send(tryagain(answer, 'Calendar.Location'));
    });
  }
} else {
  next();
}

我想在这里做的是在if (!meeting.location)中有一个循环,如果checking_location保持等于none。基本上我想检查某个Json字段是否存在,如果它不存在我想继续在rl.question中提问。我的问题是代码只在第一次工作,那么即使我给另一个输入不包含必填字段我没有得到那个问题。请注意,这不是整个代码,但它足以理解我的实现中可能存在的问题点。

回答如下:

需要在某处调用getentitiesByType,只需将其分配给变量就不会使函数运行:getentitiesByType(youArr, yourType)

另外,作为旁注,不要使用checking_location的字符串值,只需重命名变量并使用布尔值。例如:var hasLocation = false

发布评论

评论列表 (0)

  1. 暂无评论