Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:无法读取属性';foo';未定义的_Javascript_Jquery_Json_Wikipedia Api - Fatal编程技术网

Javascript 未捕获类型错误:无法读取属性';foo';未定义的

Javascript 未捕获类型错误:无法读取属性';foo';未定义的,javascript,jquery,json,wikipedia-api,Javascript,Jquery,Json,Wikipedia Api,我正在用维基百科媒体API做一个项目。我正在使用Jquery查询API。以下请求在检查Chrome中的开发控制台时起作用,如本代码笔中所示 当使用for…in循环遍历结果时,此代码有效。当编写相同的代码试图访问下面这样的结果变量时,控制台会打印一个uncaughttypeerror:cannotreadproperty'pageid'of undefined success: function (x) { var result = x.query.pages; var item

我正在用维基百科媒体API做一个项目。我正在使用Jquery查询API。以下请求在检查Chrome中的开发控制台时起作用,如本代码笔中所示

当使用for…in循环遍历结果时,此代码有效。当编写相同的代码试图访问下面这样的结果变量时,控制台会打印一个
uncaughttypeerror:cannotreadproperty'pageid'of undefined

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

  }
});

知道为什么会这样吗?为什么无法通过
result[0]
result[1]
,…,访问JSON对象的每个键<代码>结果[n]?

结果对象的键是非常大的整数(不是0、1、2)<代码>结果不是数组,而是JS对象。因此,索引不以0开头

for (var i in result)

。。迭代对象中的每个键,因此
result[i]
有效。请参阅以供参考。

我认为您可以这样记录结果:

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

result[0]不能有名为foo的键如果它是一个项目

在您的示例中,您尝试执行
result[0]
读取名为
result
的数组的第一个元素。由于
results
是一个对象,因此
result[0]
表示您试图访问
result.0
,在本例中,它并不总是存在


使用
result[0].pageid
您正在尝试访问
result.0.pageid
,如果
result.0
不存在,它显然不存在。因此,您将收到错误“Uncaught TypeError:无法读取未定义的属性'pageid'。

只需放入一些控制台日志,如下面代码中所述,您就会明白为什么会遇到此错误

    $.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;
        }
    }
 });

因此,您在这里尝试的是通过索引访问非数字对象属性,这是不可能的。如果您想这样做,那么您可以了解更多信息。

键似乎是字符串(结果是字典)。能否添加
console.log(result)
的输出?您无法通过
index
访问它们,因为
x.query.pages
返回一个JSON对象,而不是
数组的类型。只有
数组
元素可以通过
索引
访问。typeof(x.query.pages)提供了一个对象而不是数组…您所说的“结果对象的键是非常大的整数(不是0、1、2)”的确切含义是什么?请查看结果对象。键/属性是六位数字。在这里,“属性”可能是一个更好的词
    $.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;
        }
    }
 });