[英]How to get the values from an Array with server.get? (JavaScript)
So far I could save my queries into the database with post-request.到目前为止,我可以使用 post-request 将我的查询保存到数据库中。
let database = [
{'email': 'h@v.d', 'name': 'Vu', 'vorname': 'Hai'},
{'email': 'm@h.d', 'name': 'Heim', 'vorname': 'Manuel'},
{'email': 'h@v.d', 'name': 'Heinrich', 'vorname': 'Leon'},
];
server.post('/MyWebApp/mydatabase', function(req, res){
let p_mail = req.body.email;
let p_name = req.body.name;
let p_nachname = req.body.nachname;
console.log(database);
database.push({'email': p_mail, 'name': p_name, 'nachname': p_nachname});
console.log(database);
});
I can enter name, surname and e-mail and save them.我可以输入姓名、姓氏和电子邮件并保存它们。
<script>
function save() {
let in_email = document.getElementById("mail").value;
let in_name = document.getElementById("name").value;
let in_nachname = document.getElementById("nachname").value;
$.post('http://localhost:2019/MyWebApp/mydatabase',
{ 'email': in_email, 'name': in_name, 'nachname': in_nachname },
function (data) {
console.log('Object: ' + data);
}
);
}
</script>
<table>
<tr>
<td>E-Mail:</td>
<td><input type="text" size="80" id="mail" /></td>
</tr>
<tr>
<td>Vorname:</td>
<td><input type="text" size="80" id="name" /></td> <
</tr>
<tr>
<td>Nachname:</td>
<td><input type="text" size="80" id="nachname" /></td>
</tr>
<tr>
<td><input type="button" value="Ausgeben" onClick="get();" /></td>
<td><input type="button" value="Speichern" onClick="save();" /></td>
</tr>
</table>
So my question is now: If I put the e-mail in the email field and click on "send" I should get the name and surname for that e-mail.所以我现在的问题是:如果我将电子邮件放在电子邮件字段中并单击“发送”,我应该得到该电子邮件的姓名。
I have no idea how to do it...我不知道该怎么做...
You should create new GET endpoint in the server file and than from the client you can send an request with email as a query parameter.您应该在服务器文件中创建新的 GET 端点,然后从客户端发送请求,并将电子邮件作为查询参数。 ( https://en.wikipedia.org/wiki/Query_string ). ( https://en.wikipedia.org/wiki/Query_string )。
Also, you should make a logic in this endpoint GET on the server.此外,您应该在服务器上的此端点 GET 中创建一个逻辑。 You can take a look here: Find object by id in an array of JavaScript objects您可以在这里查看: 在 JavaScript 对象数组中按 id 查找对象
Server服务器
server.get('/MyWebApp/getuser', function(req, res){
let p_mail = req.query.email;
let user = database.find(x => x.email === p_mail);
res.send(user);
});
Client客户
function getUser() {
let in_email = document.getElementById("mail").value;
$.get("http://localhost:2019/MyWebApp/getuser", { mail: in_email });
}
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.