【问题标题】:Jest is throwing error > Type Error : Cannot read property 'user_env_variable is undifined开玩笑抛出错误> Typeerror:无法读取属性'use_env_variable未定义
【发布时间】:2019-08-08 12:05:06
【问题描述】:

在使用 Jest 测试应用程序时,我收到 Type Error : Cannot read property 'user_env_variable is undefined 错误。

因为'user_env_variable is undefined'错误我已经修改了if else check

if(config.user_env_variable) {...} else {...}

if(config.user_env_variable!== undefined) {...} else {...}

但我仍然遇到同样的错误。

这就是我的 dbconfig.json 的样子

dbconfig.json

{
  "development": {
    "username": "dev",
    "password": "******",
    "database": "userdb",
    "host": "user-management-dev-xxxx",
    "port": "5432",
    "dialect": "postgres"
  }
}

非常感谢任何形式的帮助。

【问题讨论】:

  • 试试typeof 运算符。像这样if(typeof config.user_env_variable!== "undefined") {...} else {...}
  • @Sagar 即使在使用 typeof 之后我仍然遇到同样的错误
  • 你用"undefined"作为字符串还是关键字?
  • 我正在使用关键字

标签: javascript node.js jestjs


【解决方案1】:

使用这个:

if(typeof(config.user_env_variable==="undefined"))

或者使用 typeof undefined 这会解决你的问题

【讨论】:

  • 好的,我理解使用这个:typeof(config.user_env_variable) === "undefined"
【解决方案2】:

试试typeof 运算符。像这样的东西。检查undefined 是字符串而不是关键字。

if(typeof config !== "undefined" && typeof config.user_env_variable!== "undefined") {...} else {...}

【讨论】:

  • 您能否使用console.log(config) 记录您的config 对象。并检查它是否是user_env_variableuse_env_variable
  • 我这样做了,但没有use_env_variableuser_env_variable 存在
  • 那么确定你的config 对象是未定义的。以上答案已更新。请检查。
  • 太棒了!你的解决方案奏效了。但是出现了一些新的问题
  • var sequelize = new Sequelize(config.database, config.username, config.password, config); 现在在此代码中将出现错误 config.database 未定义。我注意到jest test 给出了错误,但该值显示在 dev run (nodemon) 中。
猜你喜欢
  • 2017-10-18
  • 2023-03-03
  • 2020-11-19
  • 2018-06-17
  • 1970-01-01
  • 2020-07-10
  • 2017-12-15
  • 2020-06-26
  • 1970-01-01
相关资源
最近更新 更多