#cannot post /api/additemtocart
70 messages · Page 1 of 1 (latest)
axios.post("http://localhost:64135/api/additemtocart", {
userID: 2,
itemID: 2,
quantity: 1
})
.then((response) => {
console.log(response);
})
.catch(err => console.error(err))
just returns a 404
AxiosError: Request failed with status code 404
can i send you the code to the whole express file in dms
if that helps
well 404 is good, because it means it's reaching the server but your server isn't matching the path
are you sure you imported the file into the router
i have it all in one file
oh lol i can't believe i missed that
you've got a typo, you'll kick yourself when you see it
been there 😂
THANK YOU SO MUCH
sal good
@teal nacelle not to bother u but since that compiled i got another problem lol
sorry if this is a very elementary problem i normally use py kinda new to js
when im trying to parse the request body
i get
SyntaxError: "[object Object]" is not valid JSON
this is when trying to access something inside req.body
if i do req.body.<element> i get undefined
okay so you know how JS is like super unsafe when it comes to types
yea heard about that
if req.body is an object, and you try to treat it like a string, the string you get is [object object]
JSON.parse is a function that takes a string and converts it to an object
if i did this wouldnt the entire point of the line make no sense
because im converting something to a string then right back
beat me to it
haha
req.body should have properties from the post request, assuming you put it through middleware the request may already be parsed for you
i just did app.use(express.json());
but if you wanna do it the caveman way and just see what's going on inside req.body, use console.dir(req.body)
console.log will be useless since you'll get [object object] again dir will expand it just fine
got this
wait tf why cant i use req.body.userID then
wait nevermind im so stupid im so sorry lmfao
req.body.userID worked perfectly fine
i prob missed the "s" on control s when i was testing it earlier
tysm man
lifesaver
np glad you got it going
what do i do if this just prints out {}
even when i pass in data to the request
sorry idk why it just randomly stopped working lol
you sure you're passing data in?
not forgetting to attach name attributes to the form elements etc
axios.get("http://localhost:64135/api/getcarttotal", {
userID: 3
})
.then((response) => {
console.log(response);
})
.catch(err => console.error(err))
oh
this seems to be right
get requests don't have bodies
either change it to be a req.param like
http://localhost:64135/api/getcarttotal&userID=3
or change to post
i'd still use POST personally but there's no wrong answer
alright ty again
np
your problem was solved ?