我在Express应用程序的类中定义了一个javascript文件。import dotenv from 'dotenv';dotenv.config();class Settings { static getSettings() { const activeEnvironment = process.env.NODE_ENV; console.log('active ', activeEnvironment) const settings = { development: { databaseName: 'foods', }, production: { databaseName: 'foods', }, test: { databaseName: 'testdb' } }; settings[activeEnvironment] // returns undefined return settings[activeEnvironment]; }}export default Settings;但是我的问题是settings[activeEnvironment]回报undefined。我已经NODE_ENV在我的启动脚本中正确导出了set NODE_ENV=production & node --require @babel/register ./bin/www任何人都可以指出我做错了什么。我对javascript有点陌生。
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
您需要检查const activeEnvironment = process.env.NODE_ENV;3个值之一的值 development, production, test。
如果该值不在此列表中,settings[activeEnvironment]则将返回undefined。
const activeEnvironment = 'test';
console.log('active ', activeEnvironment)
const settings = {
development: {
databaseName: 'foods',
},
production: {
databaseName: 'foods',
},
test: {
databaseName: 'testdb'
}
};
console.log(settings[activeEnvironment])
添加回答
举报
0/150
提交
取消