简体   繁体   English

在express.js中定义常量

[英]Defining constants in express.js

I want to declare a key value objects in constant file and then import them. 我想在常量文件中声明一个键值对象,然后将其导入。 So, this is what I wrote: 所以,这就是我写的:

let ACCEPTABLE_LANGUAGES = 
    [
        'eng' : {'wahid'},
    ];

export default {
    ACCEPTABLE_LANGUAGES : ACCEPTABLE_LANGUAGES
}

I am importing it: import common_constants from '../common_constants'; 我正在导入: import common_constants from '../common_constants'; and using as follows: 并使用如下:

console.log(common_constants.ACCEPTABLE_LANGUAGES);

But the declaration of ACCEPTABLE_LANGUAGES gives me error like: 但是ACCEPTABLE_LANGUAGES的声明给了我这样的错误:

Unexpected token, expected : (3:18)
  1 | let ACCEPTABLE_LANGUAGES = 
  2 |   [
> 3 |       'eng' : {'wahid'},
    |                       ^
  4 |   ];

I want to make it object not array. 我想使其对象不是数组。 Why I am getting the issue? 为什么我遇到问题了?

Problem lies here: 'eng' : {'wahid'} . 问题就在这里: 'eng' : {'wahid'} In javascript {} is called an object literal. 在javascript {}中称为对象文字。 So, an object by definition should have a key and value. 因此,根据定义,对象应具有键和值。 Also, Array will not have key and pair, which you are doing in your example. 同样, Array将没有您在示例中所做的键和对。 You can do it like below: 您可以按照以下方式进行操作:

let ACCEPTABLE_LANGUAGES = 
    [
        { 'eng': 'wahid'},
    ];

export default {
    ACCEPTABLE_LANGUAGES: ACCEPTABLE_LANGUAGES
}

Now, ACCEPTABLE_LANGUAGES is array of object. 现在,ACCEPTABLE_LANGUAGES是对象数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM