working with api's using fetch
I am trying to figure out why this is not working properly. I keep getting undefine when I console.log(data)? and when I do something to mess it up on purpose the browser the .catch statment says it is wrong but I copied it from the docs and the only difference is that I put er instead of error
23 Replies
my url is also okay but here it is
you're mixing await and chaining promises
try this
yes I am confused.
okay why would the fetch doc mess me up lol
so, you can work with promises two ways, using async...await, or using .then. You were using both
so if I did it with a promise I should not use async await?
exactly
though async await is just syntactic sugar to handle promises, they're still promises in both situations
okay but I just get errors when I do it with the .then()
so I am copyin gyour stuff now 🙂
Thank you 🙂
no worries, glad to help!
I dont know what I am doing I thought I new how but when I start doing it I am like umm everything not working back to the drawing board lmao
haha, that's very familiar, especially when you've mostly been using tutorials to learn! The step to making it yourself is pretty big and people underestimate it.
Your best bet is to read the error messages and go from there, and don't hesitate to ask early and often!
well that and I am trying to figure out how to get he data and I have not used api in over year and use to axios. I was trying hard to use fetch but it is a bit confusing I realized what I wanted was to do
const data = await response.json();
and I get the data I need to parse lol
also this api I am using is using time weird like a whole number
that's a unix timestamp. Basically the number of seconds since January 1st 1970. A lot of computers work with that type of time, because it's much easier to do math on
Yeah I am trying to make that a simple date but it gives me that date you just put lol
so... Javascript is a little bit weird. Almost every other programming language uses seconds since 1970-01-01 0:00:00 UTC, Javascript uses _micro_seconds
this should work and give you a reasonable time:
it's supposed to be 3:15pm today, so about 15 minutes ago in UTC
oh okay so it is not a date?
it's a date and a time combined
so would i have to do Date(data.time)
it's indicating a specific second on a specific day, namely the one 1.7billion (plus change) seconds since January 1st 1970
yes, correct. (also good catch on the syntax, I was not paying attention and writing PHP and JS mixed together!)
oh lmao I use to know how that felt I prefer .net mvc over react and all that I hate react lol
you're in good company on this server, most people here don't really like React either 🙂
if you just want to use a framework for fun, I'd recommend using Sveltekit. It's a lot more developer friendly
I don't like the js frameworks they making a check hard like check to see if it is a password is like 15 lines all you ahve to do is
I will get into that after I get this down, I need to learn again I took like 7 months off and did a little css here and there and forgot a lot lol
the date thing is crazy if I put Date(data.time) I get todays date, if I put new Date with the time I get the 1970 LOL what LOL
i forgot the * 1000 lol
yup, that one's definitely important 🙂
so now it is time to extract everything and put it where I want it lol