1

So far I could save my queries into the database with 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...

Haidepzai
  • 764
  • 1
  • 9
  • 22

1 Answers1

1

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. (https://en.wikipedia.org/wiki/Query_string).

Also, you should make a logic in this endpoint GET on the server. You can take a look here: Find object by id in an array of JavaScript objects

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!

Petr Jelínek
  • 1,259
  • 1
  • 18
  • 36