why is typeof not checking if a variable is undefined with the below snippets:
if(typeof res.data.data[1].name !== undefined){
//the idea is that if code gets here it means it contains some data
.......
}
with the above check I still get this outcome at the if block
TypeError: Cannot read property 'name' of undefined
这可能是因为res.data.data为null,只需添加一个null检查
if(res.data.data && typeof res.data.data[1].name !== undefined){
It is happening because res.data.data[1]
is undefined itself. I'd suggest expand your condition to something like:
const { data = [] } = res.data;
if (data[1] && typeof data[1].name !== 'undefined') {
// Do somehing
}
Also your check is incorrect as you are comparing typeof result with undefined
while it returns a string, in this case 'undefined'
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.