So I am trying to make a form that when it is submitted this code is called.
async function addListing() {
let listing: any = {
make: make,
model: model,
year: year,
mileage: mileage,
price: price
};
await fetch("http://localhost:8080/api/listing", {
method: "POST",
headers: {"Content-type": "application/json"},
body: JSON.stringify(listing)
})
.then(() => {
fetch("http://localhost:8080/api/listing")
.then(res => res.json())
.then(data => {
setListingID(data[data.length - 1].id);
})
.catch(error => {
console.log(error);
});
})
.then(() => {
console.log("Listing: ", listingId, " Seller: ", sellerId)
})
.catch(error => {
console.log(error)
})
}
The post function works fine however the get function inside the first then() doesn't work the first time the button it is clicked. So the first time I click on the submit button I get: "Listing: Seller: 3" And the second time (and each time after that) it is correct for example: "Listing: 123 Seller: 3"
I tried all of yesterday to find a solution I made a separate function and it wasn't working Today I searched up the whole internet tried a bunch of things but I can't seem to find what I did wrong.
When I get this to work I want to use that listingId function in order to call another function which will connect the listingId and sellerId in a separate table.