I’m using react to send date to server but there is an error
{message: ‘Invalid date format. Date should be in the format YYYY-MM-DD.’, field: ‘from’}
I’m completely sure the data is valid and here is the code
const handleFilter = async (e) => {
e.preventDefault();
console.log(fromDate);
console.log(toDate);
try {
const date = {
from: dayjs(fromDate).format("YYYY-MM-DD"),
to: dayjs(toDate).format("YYYY-MM-DD"),
};
console.log(date);
const res = await newRequest.get("/analysis", date);
console.log(res);
} catch (error) {
console.log(error);
}
};
and there is the console
I’m tried a lot of answers like
JSON.stringify(date);
before sending
nothing work !!!
2
Answers
are you sure your server is expecting this format ?
try simply using fromdate.format("YYYY-MM-DD")
or use can use moment()
but first import it
The params you’re sending through the GET request by Postman are called "body" in the Postman nav bar. But doing a GET request, you could’nt use "body" like you usually do when you’re doing a POST request.
When you’re doing a GET request you should to pass the params instead body whitin the second param of "newRequest.get" method.
Wich will be on the request query params:
Then, once "newRequest" is an Axios instance, you should do like this:
Your code will be like this:
Hope this helps