简体   繁体   English

尝试从 MediaWiki API 获取一条数据时出现“类型错误:无法读取未定义的属性 '0'”

[英]Getting 'TypeError: Cannot read property '0' of undefined' when attempting to get a piece of data from MediaWiki API

I'm attempting to code something to get various attributes from the MediaWiki API.我正在尝试编写一些代码以从 MediaWiki API 获取各种属性。 However, it throws an error when I attempt to get certain data from the page, for example, the title.但是,当我尝试从页面获取某些数据(例如标题)时,它会引发错误。

        const reqDesc = (requestid) => new Promise((resolve, reject) => {
            request({
                method: 'GET',
                url: `https://example.wikia.com/api/v1/Articles/AsSimpleJson?id=${requestid}`
            }, (error, response, body) => {
                if (!error && response.statusCode === 200) {
                    return resolve(`<${body.sections[0].content[0].text}>`);
                } else if (error) {
                    return reject(`Error: ${error}`);
                } else {
                    return reject(`Response code: ${response.statusCode}`);
                }
            });
        });

(I know request is deprecated, I'm attempting to find something better.) (我知道 request 已被弃用,我正在尝试找到更好的东西。)

The above code should work, however at return resolve(`<${body.items[0].id}>`);上面的代码应该可以工作,但是在return resolve(`<${body.items[0].id}>`); , it throws 'TypeError: Cannot read property '0' of undefined' ,它抛出'TypeError:无法读取未定义的属性'0''

Instead of request , you could use Axios .代替request ,您可以使用Axios As for 'TypeError: Cannot read property '0' of undefined' , it means either sections or content is undefined.至于'TypeError: Cannot read property '0' of undefined' ,表示sectionscontent未定义。 You might want to take a look at what is inside body .你可能想看看body里面是什么。

Example:例子:

let requestid = 1;
let url = `https://example.wikia.com/api/v1/Articles/AsSimpleJson?id=${requestid}`;
axios({
  url: url,
  responseType: 'json',
})
.then(response => {
  console.log('--response--');
  let section = reponse.data.sections[0] || {};
  let content = section.content || {};
  let elem = content[0] || {};
  let text = content.text;
  return text;
})
.then(console.log)
.catch(error => {
  console.log('--error--');
  console.log(error);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError 从 Api 获取数据时无法读取未定义的属性“映射” - TypeError Cannot read property 'map' of undefined when getting data from Api TypeError:检索API数据时无法读取未定义的属性“ temp” - TypeError: cannot read property “temp” of undefined when retreiving API data AngularJS“ TypeError:无法读取未定义的属性&#39;protocol&#39;”从Web服务获取发布数据时 - AngularJS “TypeError: Cannot read property 'protocol' of undefined” When get post data from web service Tumblr API获取“未捕获的TypeError:无法读取未定义的属性”类型” - Tumblr API getting 'Uncaught TypeError: Cannot read property 'type' of undefined' 当尝试在谷歌工作表脚本中使用 e.value 时,我得到 TypeError: Cannot read property "Values" from undefined。 (第 7 行,文件“代码”) - when attempting to use e.value in google sheet script I get TypeError: Cannot read property "Values" from undefined. (line 7, file "Code") 我在 axios 补丁 api 中收到“Uncaught (in promise) TypeError: Cannot read property 'data' of undefined”错误 - I'm getting “Uncaught (in promise) TypeError: Cannot read property 'data' of undefined” error in axios patch api Vue TypeError:尝试更新 Object 属性时无法读取未定义的属性“_wrapper” - Vue TypeError: Cannot read property '_wrapper' of undefined when attempting to update Object property 捕获TypeError:尽管有条件,但无法读取未定义的属性“ get” - Getting Uncaught TypeError: Cannot read property 'get' of undefined in spite of the conditionals 获取Vue警告TypeError:无法读取未定义的属性,但存在数据 - Getting a Vue warning TypeError: Cannot read property of undefined but the data is present 定义变量时,获取 TypeError 无法读取未定义的属性“0” - Getting TypeError Cannot read property '0' of undefined when variable is defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM