Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/315186-learning-to-use-fetch/
Share on other sites

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.

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.

Hmm. I think I got was was expected:

Object

num_results: 55

page_number: 1

results: (55) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

total_num_results: 55

[[Prototype]]: Object

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.