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
。