简体   繁体   English

Index.js:46 未捕获的类型错误:无法读取未定义的属性“生活方式”

[英]Index.js:46 Uncaught TypeError: Cannot Read Property 'lifestyle' Of Undefined

I am new to jquery, I am writing an ajax call to retrieve data from an API,我是 jquery 的新手,我正在编写一个 ajax 调用来从 API 检索数据,

the function is been used on both live and local,该功能用于现场和本地,

On my local the response comes as在我的本地,响应是

var response = res;

var test = response.hearts.assessments.lifestyle            

on my live, the response comes as在我的直播中,回应是

var response = res;

var test = response.assessments.lifestyle 

it comes without [heart]它没有[心脏]

I need to implement a condition to work with both condition我需要实现一个条件来处理这两个条件

currently, I have used an if-else condition but it gives an error of undefined lifestyle on my local.目前,我使用了 if-else 条件,但它在我的本地给出了未定义生活方式的错误。

this is my code这是我的代码

var response = res;
            if(typeof(response.assessments.lifestyle) != "undefined" || response.assessments.lifestyle !== null) {
                var lifestyle = response.assessments.lifestyle;               
            }
            else{
                var lifestyle = response.hearts.assessments.lifestyle;
            }
            

But this code won't work, can someone please help Thank you..但是这段代码不起作用,有人可以帮忙吗谢谢..

Try as given按规定尝试

let response  =  response.assessments || response.hearts.assessments || [];
response = response.lifestyle ||"";

if "response.assessments" comes undefined it will check "response.hearts.assessments", If both comes undefined or empty or undefined it will return empty array second line to check lifestyle property, If it is undefined than blank string如果“response.assessments”未定义,它将检查“response.hearts.assessments”,如果两者都未定义或为空或未定义,它将返回空数组第二行以检查生活方式属性,如果未定义则为空字符串

You should check that the key "hearts" exists in response.assessments with this method您应该使用此方法检查 response.assessments 中是否存在关键的“hearts”

   var response = res;
   if("hearts" in response) {
     var lifestyle = response.hearts.assessments.lifestyle;               
   } else {
     var lifestyle = response.assessments.lifestyle;
   }
        

暂无
暂无

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

相关问题 index.js:8 未捕获类型错误:无法读取 index.js:8 处未定义的属性“addEventListener”, - index.js:8 Uncaught TypeError: Cannot read property 'addEventListener' of undefined at index.js:8, index.js:33 未捕获的类型错误:无法在 loadQuiz 中读取未定义的属性“问题”(index.js:33) - index.js:33 Uncaught TypeError: Cannot read property 'question' of undefined at loadQuiz (index.js:33) 如何解决 index.js:9 Uncaught TypeError: Cannot read property &#39;setWallpaper&#39; of undefined? - How to resolve index.js:9 Uncaught TypeError: Cannot read property 'setWallpaper' of undefined? index.js:28 未捕获类型错误:无法读取 null 的属性“addEventListener”(在 toggleActiveOnClick 和 Array.forEach 中) - index.js:28 Uncaught TypeError: Cannot read property 'addEventListener' of null ( at toggleActiveOnClick and at Array.forEach) TypeError:无法读取Gatsby中未定义的…src / pages / index.js的属性“数据” - TypeError: Cannot read property 'data' of undefined …src/pages/index.js in Gatsby Chaplin.js-未捕获的TypeError:无法读取未定义的属性“未定义” - Chaplin.js - Uncaught TypeError: Cannot read property 'undefined' of undefined TypeError:无法读取 Socket 未定义的属性“房间”。<anonymous> (C:\CHAT\server\index.js:22:21)</anonymous> - TypeError: Cannot read property 'room' of undefined at Socket.<anonymous> (C:\CHAT\server\index.js:22:21) 未捕获的类型错误:无法在 HTMLButtonElement 处设置 null 的属性“textContent”。<anonymous> (index.js:17),为什么会这样?</anonymous> - Uncaught TypeError: Cannot set property 'textContent' of null at HTMLButtonElement.<anonymous> (index.js:17), why is it happening? p5.j​​s上的“未捕获的TypeError:无法读取未定义的属性&#39;bind&#39;” - “Uncaught TypeError: Cannot read property 'bind' of undefined” on p5.js Three.js - 未捕获的TypeError:无法读取未定义的属性“normal” - Three.js - Uncaught TypeError: Cannot read property 'normal' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM