如何在node.js中使用const获取可变数据

时间:2018-03-26 12:24:07

标签: node.js api

我不知道如何在导出该const变量时访问另一个文件中的const变量。这是我的代码

在index.js文件中

const sbt_sp_add_speed_breaker = [
    "id",
    "device_key",
    "latitude",
    "longitude",
    "type",
    "start_latitude",
    "start_longitude",
    "end_latitude",
    "end_longitude"
];

module.exports.sbt_sp_add_speed_breaker =sbt_sp_add_speed_breaker;

以下是我如何访问另一个文件中的const

var schema = require('./../spschema/index');
var sp = schema.sp_name;//showing undefined error

上面的var sp显示未定义的错误,这意味着它没有访问另一个文件中的const。任何人都可以解释如何执行此操作。

6 个答案:

答案 0 :(得分:1)

您正在访问错误的变量。执行require的变量名称与导出的变量名称相同。请使用:

var schema = require('./../spschema/index');
var sp = schema.sbt_sp_add_speed_breaker;

如果您想使用var sp = schema.sp_name;,您应该:

module.exports.sp_name = sbt_sp_add_speed_breaker;

Sitepoint有一个关于JavaScript模块导出的好教程:https://www.sitepoint.com/understanding-module-exports-exports-node-js/

完整解决方案

文件结构

.
├── spschema
│   └── index.js
└── src
    └── app.js

spschema / index.js

const sbt_sp_add_speed_breaker = [
    "id",
    "device_key",
    "latitude",
    "longitude",
    "type",
    "start_latitude",
    "start_longitude",
    "end_latitude",
    "end_longitude"
];

module.exports.sbt_sp_add_speed_breaker = sbt_sp_add_speed_breaker;

的src / app.js

var schema = require('./../spschema/index');
var sp = schema.sbt_sp_add_speed_breaker;

console.log(sp);

答案 1 :(得分:1)

您的模块导出不正确。请输出如下,

module.exports = sbt_sp_add_speed_breaker

然后试试,

var schema = require('./../spschema/index');
var sp = schema.sbt_sp_add_speed_breaker;

答案 2 :(得分:0)

您只需在定义变量的位置后面添加export即可。 然后在导入时将其与.一起使用

答案 3 :(得分:0)

您要检查的第一件事是您需要该模块的目录的相对路径。您确定其他文件位于spschema目录的兄弟目录中吗?

接下来,当您需要schema时,您正试图从中获取名为sp_name的属性。在您导出的模块中,没有对象导出该属性。这只是一个阵列。尝试像console.log(schema)这样的行来查看输出是什么。如果架构未定义,那么我会说您尝试使用不正确的路径导入。

我会像这样构建你的代码:

const sbt_sp_add_speed_breaker = [ ... ]; module.exports = sbt_sp_add_speed_breaker;

// Good idea to use the same variable name for consistency const sbt_sp_add_speed_breaker = require('./path/to/module/sbt_sp_add_speed_breaker');

答案 4 :(得分:0)

sbt_sp_add_speed_breaker您从spschema/index导出的内容是array。所以,如果你打印schema.sbt_sp_add_speed_breaker。您将获得array。您需要使用.

来使用索引进行访问

<强> constants.js

'use strict';

const sbt_sp_add_speed_breaker = [
    "id",
    "device_key",
    "latitude",
    "longitude",
    "type",
    "start_latitude",
    "start_longitude",
    "end_latitude",
    "end_longitude"
]; // 1st type of constant

const USER = [
    'first_name',
    'last_name',
    'gender'
]; //2nd type of constants

const ADDRESS = [
    'door_no',
    'landmark_near_by',
    'city',
    'pincode'
]; //3rd type of constant

module.exports = {
    sbt_sp_add_speed_breaker: sbt_sp_add_speed_breaker,
    user: USER,
    address:ADDRESS
};

<强> index.js

'use strict';

let schema = require('./constants');
let _ = require('lodash');

console.log('First constant ', schema.sbt_sp_add_speed_breaker); // 1st type of constant
console.log('Second constant ', schema.user); // 2nd type of constant
console.log('Third constant', schema.address); // 3rd type of constant

console.log(`First element in 1st type of first constant array = ${schema.sbt_sp_add_speed_breaker[0]}`);
console.log(`First element in 1st type of second constant  array = ${schema.user[0]}`);
console.log(`First element in 1st type of third constant  array = ${schema.address[0]}`);

打印

  

第一个常数[&#39; id&#39;,     &#39; device_key&#39 ;,     &#39;纬度&#39 ;,     &#39;经度&#39 ;,     &#39;类型&#39 ;,     &#39; start_latitude&#39 ;,     &#39; start_longitude&#39 ;,     &#39; end_latitude&#39 ;,     &#39; end_longitude&#39; ]
  第二个常数[&#39; first_name&#39;,&#39; last_name&#39;,&#39;性别&#39; ]
  第三个常数[&#39; door_no&#39;,&#39; landmark_near_by&#39;,&#39; city&#39;,&#39; pincode&#39; ]
  第一类常量数组的第一个元素= id
  第一类第二常数数组中的第一个元素= first_name
  第一种类型的第三种常量数组中的第一个元素= door_no

答案 5 :(得分:0)

如果您想将变量导出为其他名称,请以不同方式命名,或以不同方式导入。因此,选项一,重命名您的导出:

const sbt_sp_add_speed_breaker = 'BLAH';
module.exports.sp_name = sbt_sp_add_speed_breaker; // renamed the export here

另一个选项,在导入时重命名:

const sbt_sp_add_speed_breaker = 'BLAH';
// the export remains the same
module.exports.sbt_sp_add_speed_breaker = sbt_sp_add_speed_breaker; 

// but in other.module.js, we get it differently:

const schema = require('./../spschema/index');
const sp = schema.sbt_sp_add_speed_breaker; // match the name of your export

或者,使用JavaScript模块,并更明确地了解您的导出

const sbt_sp_add_speed_breaker = 'BLAH';
export { sbt_sp_add_speed_breaker as sp_name }; // renamed the export 

// other module
import { sp_name as sp } from './the-other-file';