I keep getting an error when I run this code which says await is only valid in async function. I tried to fix it, but I think I'm putting the code in the wrong place because it's not working, I keep getting the same error message.
Login Client Side:
<script>
const logindetails = new Vue({
el: '#logindetails',
data: {
email: "",
password: "",
},
methods: {
login: function (e) {
const body = { email, password };
const response = await fetch(
"http://localhost:3000/authentication/login".then(async response => {
console.log("success");
}),
{
method: "POST",
headers: {
"Content-type": "application/json"
},
body: JSON.stringify(body)
},
);
}}})
You need to make your login function async.
methods: {
login: async function (e) {
You need to make your login function async -
methods: {
login: async function() { ...
Hi Madhu nice seeing you here again. Ok so Jakub is correct that you should put the async behind the function. Also, Remember that .then and await do the same things , so you either use one or the other. In this case, what you want to do is this
const response = await fetch(
"http://localhost:3000/authentication/login",
{
method: "POST",
headers: {
"Content-type": "application/json"
},
body: JSON.stringify(body)
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.