简体   繁体   中英

Getting console error for object return

Hi i have to disable button if i get scenariosViewAll.collectionBookObject ={} array ,For this i am using function to return below is my code:

  <button" ng-disabled="scenariosViewAll.isSetDisable(scenariosViewAll.collectionBookObject)">Create Book from Collection</button>

and in controller my code is:

 function isSetDisable(obj) {
           return typeof(obj === 'Object') && Object.keys(obj).length === 0 ? true : false;
        }

getting console error as below and it is working properly, any wrong in my code.

lib.min.js:3762 TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at scenariosViewAllController.isSetDisable (app.min.js:29729)
    at fn (eval at compile (lib.min.js:3876), <anonymous>:4:415)
    at m.$digest (lib.min.js:3787)
    at m.$apply (lib.min.js:3790)
    at lib.min.js:3803
    at e (lib.min.js:3690)
    at lib.min.js:3693

You should change Object condition and add extra condition to handle possible null values;

 function isSetDisable(obj) {
           return obj != null && typeof(obj) == typeof({}) && Object.keys(obj).length === 0 ? true : false;
        }
function isSetDisable(obj) {
           return typeof(obj) === 'object' && Object.keys(obj).length === 0 ? true : false;
        }

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