简体   繁体   中英

Strange (webpack?) error “TypeError: Object(…) is not a function”

Following syntax

const INITIAL_STATE = {
  userIsActive: getAccount() ? getAccount().status === "open" : false
};

Causes browser to throw TypeError: Object(...) is not a function error, I pinpointed it to being syntax specific, getAccount() just returns object like

{
  status: "open"
}

Changing to this works perfectly fine, even returns correct data

const accStatus = () => {
  try {
    return getAccount() ? getAccount().status === "open" : false;
  } catch (e) {
    console.error(e);
    return false;
  }
};

const INITIAL_STATE = {
  userIsActive: accStatus
};

but I don't understand why it doesn't work in the first place?

EDIT: That catch statement is not triggered, which is odd

In the first example, userIsActive is a boolean value, whereas in the second example it is a function that returns the boolean. This will probably work:

const INITIAL_STATE = {
  userIsActive: () => getAccount() ? getAccount().status === "open" : 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