-1

I'm doing a project with the wikipedia media API. I'm querying the API using Jquery. The following request works when checking the developper console in Chrome as seen in this codepen http://codepen.io/jowze/pen/NNZNKY/?editors=1010

$.ajax({
 url:'https://en.wikipedia.org/w/api.php',
  data: { action : 'query', generator: 'search', gsrnamespace : '0', gsrlimit : '10', prop : 'extracts', pilimit: 'max',
 exintro: '', explaintext: '', exsentences: '1' , exlimit: 'max' , gsrsearch: 'Richard Feynman', format: 'json'},
  dataType: 'jsonp',

  success: function (x) {
  var result = x.query.pages;  
    for (var i in result) {
    var item = result[i].pageid;
  console.log(item);

    }
  }
});

This code works when looping through the result with a for...in loop. When writing the same code trying to access the result variable like below, the console prints an Uncaught TypeError: Cannot read property 'pageid' of undefined

 success: function (x) {
  var result = x.query.pages;  
    var item = result[0].pageid;
  console.log(item);

  }
});

Any idea why this is happening? Why is it impossible to access each key of the JSON object via result[0], result[1],... ,result[n]?

Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
jowze
  • 63
  • 1
  • 3
  • The keys seem to strings (results is a dictionary). Could you add the output of `console.log(result)`? – martianwars May 31 '16 at 10:27
  • 2
    You are not able to access them via `index` because `x.query.pages` returns you a JSON object and not of the type `Array`. Only `Array` elements can be accessed by `index`. – vijayP May 31 '16 at 10:27
  • typeof(x.query.pages) gives an object and not an array... – gkb May 31 '16 at 10:32

4 Answers4

0

The keys of your result object are very large integers (not 0, 1, 2). results is not an array, it's a JS object. Hence indices don't start with 0.

for (var i in result)

.. iterates over every key in the object and hence result[i] works. See MDN for reference.

martianwars
  • 6,380
  • 5
  • 35
  • 44
  • what exactly you mean by "keys of your result object are very large integers (not 0, 1, 2)"? – vijayP May 31 '16 at 10:32
  • Have a look at the results object. The keys / properties are six digit numbers. Probably "properties" is a better word here – martianwars May 31 '16 at 10:34
0

I think you can log what it is result like this:

success: function (x) {
  var result = x.query.pages;  
      var item = result[0].pageid;
      console.log(result,result[0]);
  }
});

result[0] mustn't has a key named foo if it's a project

hunter2009
  • 133
  • 3
0

In your example, you tried to do result[0] which reads the first element of an array called result. Since results is an object, thus result[0] means you are trying to access result.0 which in this case does not always exist.

With result[0].pageid you are trying access result.0.pageid which obviously won't exist if result.0 doesn't exist. Therefore, you will get the error "Uncaught TypeError: Cannot read property 'pageid' of undefined".

csukcc
  • 752
  • 7
  • 8
0

Just put some console logs as mentioned in the below code and you will understand why are you facing this error.

    $.ajax({
    url:'https://en.wikipedia.org/w/api.php',
    data: { action : 'query', generator: 'search', gsrnamespace : '0', gsrlimit : '10', prop : 'extracts', pilimit: 'max', exintro: '', explaintext: '', exsentences: '1' , exlimit: 'max' , gsrsearch: 'Richard Feynman', format: 'json'},
    dataType: 'jsonp',
    success: function (x) {
        var result = x.query.pages;
        console.log("== What is the type of result : " + typeof(result)); //Object
        console.log("Complete Result : " + JSON.stringify(result));
        for (var i in result) {
            console.log("Property type for object result :" + typeof(i)); //String
            var item = result[i].pageid;
            var title = result[i].title;
            var extract = result[i].extract;
        }
    }
 });

So, What you are trying to do here is to access the non-numeric object properties by index, which is not possible. If you want to do it, then you can learn more about it here.

Community
  • 1
  • 1
Gopal Yadav
  • 368
  • 2
  • 8