JavaScript 对象访问器在对象属性检查期间返回 Undefined

时间:2021-04-30 10:19:17

标签: javascript node.js

我为我的节点环境创建了一个模块脚手架。

const environments = {}

environments.staging = {
    port: 3000,
    envName: 'staging'
}

environments.production = {
    port: 5000,
    envName: 'production'
}

当我每次检查当前环境返回当前 process.env.NODE_ENV 值时

const currentEnvironment = typeof(process.env.NODE_ENV) === 'string' ? process.env.NODE_ENV : 'staging'
console.log(currentEnvironment) //staging or production

但是,当我用我的环境对象检查它时,它总是返回 undefined

const environmentToExport = typeof environments[currentEnvironment] === "object" ? environments[currentEnvironment] : environments.staging
console.log(environments[currentEnvironment]) // here returns undefined.

我的代码有什么问题?

0 个答案:

没有答案