#cannot post /api/additemtocart

70 messages · Page 1 of 1 (latest)

teal nacelle
#

catch your errors so you can see what the errors are?

#
axios.post("http://localhost:64135/api/additemtocart", {
    userID: 2,
    itemID: 2,
    quantity: 1
  })
  .then((response) => {
    console.log(response);
  })
  .catch(err => console.error(err))
sullen pumice
#

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

teal nacelle
#

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

sullen pumice
#

i have it all in one file

teal nacelle
#

oh lol i can't believe i missed that

#

you've got a typo, you'll kick yourself when you see it

sullen pumice
#

no shot

#

i looked over the entire thing for a straight hour

#

where is it??

teal nacelle
#

this is the culprit

sullen pumice
#

slash

#

WAIT

#

LMFAO

#

THATS SO DEPRESSING I JUST WASTED AHOUR OF MY LIFE

teal nacelle
#

been there 😂

sullen pumice
#

THANK YOU SO MUCH

teal nacelle
#

sal good

sullen pumice
#

@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

teal nacelle
#

okay so you know how JS is like super unsafe when it comes to types

sullen pumice
#

yea heard about that

teal nacelle
#

if req.body is an object, and you try to treat it like a string, the string you get is [object object]

sullen pumice
#

so would i do

#

json.parse(json.stringify

teal nacelle
#

JSON.parse is a function that takes a string and converts it to an object

sullen pumice
#

because im converting something to a string then right back

teal nacelle
#

beat me to it

sullen pumice
#

haha

teal nacelle
#

req.body should have properties from the post request, assuming you put it through middleware the request may already be parsed for you

sullen pumice
#

i just did app.use(express.json());

teal nacelle
#

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

sullen pumice
#

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

teal nacelle
#

np glad you got it going

sullen pumice
#

even when i pass in data to the request

#

sorry idk why it just randomly stopped working lol

teal nacelle
#

you sure you're passing data in?

#

not forgetting to attach name attributes to the form elements etc

sullen pumice
#
axios.get("http://localhost:64135/api/getcarttotal", {
    userID: 3
  })
  .then((response) => {
    console.log(response);
  })
  .catch(err => console.error(err))
teal nacelle
#

oh

sullen pumice
#

this seems to be right

teal nacelle
#

get requests don't have bodies

sullen pumice
#

oh

#

so when im doing something like

#

getting the total for a shopping cart

teal nacelle
#

either change it to be a req.param like

http://localhost:64135/api/getcarttotal&userID=3

or change to post

sullen pumice
#

do you think i should do a get request then put in the cart id as a parm

#

oh alr

teal nacelle
#

i'd still use POST personally but there's no wrong answer

sullen pumice
#

alright ty again

teal nacelle
#

np

pure tundra
#

your problem was solved ?