简体   繁体   中英

TypeError: Cannot read properties of undefined (reading 'name');

Error:

Cannot read properties of undefined (reading 'name')

Code:

function loop(){
    request.get({
        url: 'https://games.roblox.com/v1/games?universeIds=3652651589',
        rejectUnauthorized: false,
        headers: {
          'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3750.0 Iron Safari/537.36'
        }
      }, (err, res, body) => {
        console.log(body.data.name)
      });

    setTimeout(loop, 5000);
}

loop();

What must I do?

data is an array , you can not do things like that data.name
You need to use some array methods to access the information in the data or simply to specify exact index in the array in this case is 0 because the data array has only one item in it, but this is kind of not scalable...

body.data.map((currentElement) => { console.log(currentElement.name) }
and "On Development" should be printed.

body.data.name替换res.data.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM