+ 5

Cat API

Okay, so I did .json() method and used the .file cus thats what it said in the url. I dont know what it wrong here and why the cat is not showing https://code.sololearn.com/WhAgtcxgmaKM/?ref=app

14th Jun 2022, 4:07 PM
Junior
Junior - avatar
37 odpowiedzi
+ 2
data = [{...}] data[0] = {...}
14th Jun 2022, 11:52 PM
Simon Sauter
Simon Sauter - avatar
+ 3
My bad in my haste to assist I missed some other things with your code. I commented them in the code below. https://code.sololearn.com/Wza4BQ2ITA98
14th Jun 2022, 6:58 PM
ODLNT
ODLNT - avatar
+ 3
From the API documentation: "Quickstart - Get a random Kitty Using the language / framework of your choice: Load https://api.thecatapi.com/v1/images/search Get the first Array object of the JSON response Load it’s .url Enjoy the kitty 😺 Bonus: Add a button to repeat the request for a new kitty!" (https://docs.thecatapi.com/) The important part for your last question is "Get the first Array object..." therefore you have to use 0 as index. It's just the way the data is structured in the json. Every API should come with documentation of some kind. When none is available you have to look at the file and figure out the structure yourself.
14th Jun 2022, 8:30 PM
Simon Sauter
Simon Sauter - avatar
+ 3
We're talking about the brackets at the start. // initial array (data) [ // object is the first element of the array at data[0] { // properties of the object "breeds":[], "categories":[{"id":7,"name":"ties"}], "id":"u1", "url":"https://cdn2.thecatapi.com/images/u1.jpg",// data[0].url "width":625, "height":446 } ]
14th Jun 2022, 10:34 PM
ODLNT
ODLNT - avatar
+ 3
data[0] gives you the braces and everything between (breeds to height).
14th Jun 2022, 11:30 PM
Simon Sauter
Simon Sauter - avatar
+ 3
Your Mom data ----> [{ ... }] <-array array is accessed by index data[0] ----> { ... } <-object object is accessed by key data[0].url ----> "https....jpg" examine what the api returns and process them appropriately. fun api😁 https://code.sololearn.com/W4wFF883v1V2/?ref=app
15th Jun 2022, 2:14 AM
Bob_Li
Bob_Li - avatar
+ 2
I do not know. Is that what you got when you searched it up? If so, it may be best to use a different API.
14th Jun 2022, 4:25 PM
Justice
Justice - avatar
+ 2
For me it is definitely saying something. It says the error one you press the New Cat button.
14th Jun 2022, 4:47 PM
Justice
Justice - avatar
+ 2
YIPMONG This thread is not for you to post your own post because you are not answering the question of this post
16th Jun 2022, 2:23 AM
JTLZ
+ 1
Always make sure to check the console when you're not getting exactly what you want. You're getting an accessed blocked error. You can type the error into Google and you might be able to find what the problem is.
14th Jun 2022, 4:21 PM
Justice
Justice - avatar
+ 1
Did you check your console?
14th Jun 2022, 4:39 PM
Justice
Justice - avatar
+ 1
In your js code showNewCat(url) it requires an URL, but in HTML an url is not passed to showNewCat() when the button is clicked. Why?
14th Jun 2022, 6:10 PM
ODLNT
ODLNT - avatar
+ 1
Yes I see that and that url will be used for that call to the function and when the function is done that instance of the function is thrown away. When you press the button you are invoking a new instance of showNewCat function and it need an url also.
14th Jun 2022, 6:36 PM
ODLNT
ODLNT - avatar
+ 1
The quick fix is to pass the url via the onclick attribute onclick="showNewCat('https://api.thecatapi.com/v1/images/search')"
14th Jun 2022, 6:40 PM
ODLNT
ODLNT - avatar
+ 1
Okay thanks just one thing. the array part. why is the data [0]? data[0]. Why is that number 0 is what im asking. why cant it be 3 or 1
14th Jun 2022, 8:04 PM
Junior
Junior - avatar
+ 1
The API returns an array with one element and array indexes start with 0. This means that the array's first and only element has an index of 0. Hence data[0]. If I were to use data[3] or data[1], undefined would be returned. https://www.w3schools.com/js/js_arrays.asp
14th Jun 2022, 9:05 PM
ODLNT
ODLNT - avatar
+ 1
okay if im correct wouldnt that mean [0] is the breeds?
14th Jun 2022, 10:35 PM
Junior
Junior - avatar
+ 1
hm? how so? Wouldnt 0 mean breeds? I tested arrays on w3schools and it only showed the “0” object.
14th Jun 2022, 11:33 PM
Junior
Junior - avatar
15th Jun 2022, 3:22 PM
Junior
Junior - avatar
0
so theres some kind of malware in that api website?
14th Jun 2022, 4:23 PM
Junior
Junior - avatar