gw1500se Posted August 15, 2022 Share Posted August 15, 2022 I am just starting to learn to use 'fetch'. I have the following script: function requests(url) { fetch(url) .then(response => { // indicates whether the response is successful (status code 200-299) or not if (!response.ok) { throw new Error(`Request failed with status ${response.status}`) } return response.json() }) .then(data => { console.log(data.count) console.log(data.products) }) .catch(error => console.log("Auto_select: "+error)) } var json_formatted_str, obj; console.log("Getting page data"); json_formatted_str = requests("https://worker.mturk.com/projects.json"); I kind of gleaned this from an example but am not understanding what I am doing. The URL returns data in json format and I want the function to return that data so I can process it. I think it is not returning any data but simply writing it to the console. In addition, I get an undefined error on 'console.log(data.count)'. Can someone explain what this is doing and how to simply return the json data to the main code? TIA. Quote Link to comment https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/ Share on other sites More sharing options...
requinix Posted August 15, 2022 Share Posted August 15, 2022 Are you sure that URL is returning what you expect? What do you see if you console.log the whole data object? Quote Link to comment https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/#findComment-1599393 Share on other sites More sharing options...
gw1500se Posted August 15, 2022 Author Share Posted August 15, 2022 Thanks for the reply. I'm not quite sure how to syntactically do that. I added console.log(data) right after the return and got nothing other than the same undefined error on data.count. I guess that means I ether cannot log it there or response is returning nothing but neither is it giving an error. Quote Link to comment https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/#findComment-1599394 Share on other sites More sharing options...
kicken Posted August 15, 2022 Share Posted August 15, 2022 3 minutes ago, gw1500se said: I added console.log(data) right after the return You can't put your logging code after the return statement. The function stops at the return. Change your second .then handler to just be .then(data => console.log(data)) and see what it shows. That or look at the request in the networking tab of the dev tools to see what the response is. Quote Link to comment https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/#findComment-1599395 Share on other sites More sharing options...
gw1500se Posted August 15, 2022 Author Share Posted August 15, 2022 Hmm. I think I got was was expected: Object num_results: 55 page_number: 1 results: (55) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] total_num_results: 55 [[Prototype]]: Object Quote Link to comment https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/#findComment-1599396 Share on other sites More sharing options...
kicken Posted August 15, 2022 Share Posted August 15, 2022 25 minutes ago, gw1500se said: Hmm. I think I got was was expected If that's from console.log(data) then that would indicate that you have an object with the properties num_results, page_number, results, and total_num_results. No count or products properties. Quote Link to comment https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/#findComment-1599397 Share on other sites More sharing options...
gw1500se Posted August 15, 2022 Author Share Posted August 15, 2022 Go it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/#findComment-1599398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.