d3.json callback errors

285 views
Skip to first unread message

Joe Keohan

unread,
Oct 25, 2016, 10:36:29 AM10/25/16
to d3-js
I'm working on creating a viz that needs to make an api call to google translate.  d3.json is pulling geojson data and I'm trying to use a callback to make an ajax call for each item, however many of the countries fail when an attempt is made to translate and that breaks the callback and subsequent d3 code.  I was hoping the ajax call would handle the error and continue on but thats not the case.  Is there a good workaround to this? 

function update(d){
  d.features.forEach(function(d) {
    var code = d.properties.iso_a2.toLowerCase()
    var traslate;

    $.ajax({
      url: url,
      type: 'GET',
      success: function(data){ 
          //console.log(data)
          d.properties.hello = data.data.translations[0].translatedText
      },
      error: function(data) {            
      } 
    })

});
  return d
}

d3.json(url, update, function(err, world)  { } ) 

Joe

Seemant Kulleen

unread,
Oct 25, 2016, 10:48:16 AM10/25/16
to d3-js
Hi Joe,

It might be helpful to use the d3 equivalent of "$.ajax()".  In v3, please check out d3.xhr, and in v4, it's called d3.request.  For example, I ran this in the browser console:

d3.request("https://www.googleapis.com/language/translate/v2?key=AIzaSyAADudga5Cdk2QlHDWF8UAHQEy-Z_ikHw8&target=es&q=Hello", function(blah) { console.log(blah); })

and it output a javascript object with a response key in it (as well as a status key for determining error).

Hope that helps.
Cheers,
Seemant
 

--
Oakland Finish Up Weekend
Be Amazed.  Be Amazing.
Get Mentored | Get Inspired | Finish Up
http://oaklandfinishup.com


--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Keohan

unread,
Oct 25, 2016, 12:48:30 PM10/25/16
to d3...@googlegroups.com
Seemant,

I tested your solution and it works for the country code you provided...but it errors out with one that I know doesn't work...such as below...back to the drawing board...:}


Jo

--
You received this message because you are subscribed to a topic in the Google Groups "d3-js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/d3-js/B_fcuZpxQfA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to d3-js+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Joe Keohan

Seemant Kulleen

unread,
Oct 25, 2016, 8:10:24 PM10/25/16
to d3-js
Hi Joe,

Yes, all I did was output the received object -- as the programmer, itll be up to you to look at the returned object, and figure out what to do in the error case...

Cheers,
seemant


--
Oakland Finish Up Weekend
Be Amazed.  Be Amazing.
Get Mentored | Get Inspired | Finish Up
http://oaklandfinishup.com


Reply all
Reply to author
Forward
0 new messages