英文:
Show an alert if the value ("email") was not found in the input search
问题
你好,以下是您的代码的中文翻译:
async function search() {
const email = document.getElementById("email").value.trim();
if (!email) return alert("错误。");
const url = `https://sheetdb.io/api/v1/i3dp73wvi19kf/search?EMAIL=${email}`;
const token = "我的API密钥";
const method = "GET";
try {
const res = await fetch(url, {
method,
headers: {
"Authorization": `Bearer ${token}`
}
});
if (!res.ok) throw new Error("结果不正常");
const data = await res.json();
// 如果API没有响应,可以使用以下数据生成结果
var jsondata = [{
"CARGO": "JEFE",
"CIUDAD": "CIUDAD1",
"EMAIL": "jperez",
"Función": "OPERACIONES",
"NOMBRE": "JUAN PEREZ",
"Unidad organizativa": "OPERACIONES"
}]
let text = ""
var output = document.getElementById("output");
output.innerHTML = "搜索中...";
var keys = [];
text += "<table border==\"1\"><tr>";
for (key in data[0]) {
text += '<td>' + key + '</td>';
}
text += "</tr>";
for (var i = 0; i < data.length; i++) {
text += '<tr>';
for (key in data[i]) {
text += '<td>' + data[i][key] + '</td>';
}
text += '</tr>';
}
text += "</table>";
output.innerHTML = text;
} catch (err) {
alert(err);
}
}
<form onsubmit="search(); event.preventDefault(); return false">
<label>EMAIL: <input type=search id="email"></label>
<button type=submit>搜索</button>
</form>
<hr />
<div id=output></div>
希望这对您有所帮助!如果您有其他问题,请随时提问。
英文:
I have a function that looks for an email and displays the data from an api call. I want to display an alert('Email not found') if the email does not exist in the data. How can I achieve this?
This is my code that I have
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
async function search() {
const email = document.getElementById("email").value.trim();
if (!email) return alert("error.");
const url = `https://sheetdb.io/api/v1/i3dp73wvi19kf/search?EMAIL=${email}`;
const token = "my-api-key";
const method = "GET";
try {
const res = await fetch(url, {
method,
headers: {
"Authorization": `Bearer ${token}`
}
});
if (!res.ok) throw new Error("Result not OK");
const data = await res.json();
// if api didnt respond use this instead data to generate result
var jsondata = [{
"CARGO": "JEFE",
"CIUDAD": "CIUDAD1",
"EMAIL": "jperez",
"Función": "OPERACIONES",
"NOMBRE": "JUAN PEREZ",
"Unidad organizativa": "OPERACIONES"
}]
let text = ""
var output = document.getElementById("output");
output.innerHTML = "Searching...";
var keys = [];
text += "<table border==\"1\"><tr>";
for (key in data[0]) {
text += '<td>' + key + '</td>';
}
text += "</tr>";
for (var i = 0; i < data.length; i++) {
text += '<tr>';
for (key in data[i]) {
text += '<td>' + data[i][key] + '</td>';
}
text += '</tr>';
}
text += "</table>";
output.innerHTML = text;
} catch (err) {
alert(err);
}
}
<!-- language: lang-html -->
<form onsubmit="search(); event.preventDefault(); return false">
<label>EMAIL: <input type=search id="email"></label>
<button type=submit>Search</button>
</form>
<hr />
<div id=output></div>
<!-- end snippet -->
答案1
得分: 2
你应该在函数开始处设置一个标志变量,并将其初始化为false,例如:
let emailFound = false
然后,在结果循环内,你可以将每个电子邮件与初始电子邮件进行比较,如果找到匹配项,将标志设置为true,例如:
if (data[i].email == email) {
emailFound = true
}
如果在循环结束时,标志仍然为false,这意味着未找到电子邮件,因此你可以安全地显示适当的警告消息。
英文:
You should set a flag variable at the beginning of your function and initialize it to false e.g.:
let emailFound = false
Then, inside the results loop you can compare each email with the initial one and if a match is found, set the flag to true like:
if (data[i].email == email) {
emailFound = true
}
If at the end of the loop, the flag is still false that means that the email was not found so you can safely show the suitable alert message.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论