Javascript 开玩笑就是犯错误>;类型错误:无法读取属性';用户_env_变量未定义
在使用Jest测试应用程序时,我得到一个Javascript 开玩笑就是犯错误>;类型错误:无法读取属性';用户_env_变量未定义,javascript,node.js,jestjs,Javascript,Node.js,Jestjs,在使用Jest测试应用程序时,我得到一个类型错误:无法读取属性'user\u env\u变量未定义错误 由于“user\u env\u变量未定义”错误,我已修改if else检查 if(config.user_env_variable) {...} else {...} 到 但我还是犯了同样的错误 这就是我的dbconfig.json的样子 dbconfig.json { "development": { "username": "dev", "password": "*
类型错误:无法读取属性'user\u env\u变量未定义错误
由于“user\u env\u变量未定义”
错误,我已修改if else检查
if(config.user_env_variable) {...} else {...}
到
但我还是犯了同样的错误
这就是我的dbconfig.json的样子
dbconfig.json
{
"development": {
"username": "dev",
"password": "******",
"database": "userdb",
"host": "user-management-dev-xxxx",
"port": "5432",
"dialect": "postgres"
}
}
非常感谢您提供的任何帮助。请使用以下方法:
if(typeof(config.user_env_variable==="undefined"))
或者使用typeof undefined这将解决您的问题尝试typeof
操作符。像这样的。检查未定义是否为字符串而不是关键字
if(typeof config!=“undefined”&&typeof config.user\u env\u variable!=“undefined”){…}否则{…}
您尝试过这个吗?尝试typeof
操作符。类似这样的if(typeof config.user_env_variable!==“undefined”){…}else{…}
@Sagar即使在使用typeof
之后,我仍然会遇到同样的错误你用“undefined”
作为字符串或关键字是什么?我在使用关键字你能用console.log(config)
记录你的config
对象吗。一旦它是user_env_variable
或use_env_variable
我就检查了,但没有use_env_variable
或user_env_variable
存在,然后确保config
对象未定义。以上答案已更新。请检查,太好了!你的解决方案奏效了。但是出现了一些新问题var sequelize=newsequelize(config.database,config.username,config.password,config)代码>现在在此代码中出现错误config。未定义数据库。我注意到,jest
test给出了错误,但是该值显示在dev run(nodemon)中。好的,我理解使用这个:typeof(config.user\u env\u variable)=“undefined”
if(typeof(config.user_env_variable==="undefined"))