如何在node.js中使用const获取变量数据
我不知道如何在导出另一个文件中的常量变量时访问该常量变量。这是我的代码 在index.js文件中如何在node.js中使用const获取变量数据,node.js,api,Node.js,Api,我不知道如何在导出另一个文件中的常量变量时访问该常量变量。这是我的代码 在index.js文件中 const sbt_sp_add_speed_breaker = [ "id", "device_key", "latitude", "longitude", "type", "start_latitude", "start_longitude", "end_latitude", "end_longitude" ]; modu
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显示未定义的错误,这意味着它没有访问另一个文件中的常量。有人可以解释如何执行此操作。您只需在定义变量的后面添加一个
export
。
然后在导入时将其与
一起使用首先要检查的是需要模块的目录的相对路径。您确定另一个文件位于spschema
目录的同级目录中吗
接下来,当您需要模式
时,您试图从中获取一个名为sp\u name
的属性。在要导出的模块中,没有导出具有该属性的对象。它只是一个数组。尝试执行类似于console.log(schema)
的行以查看输出是什么。若模式是未定义的,那个么我会说您试图使用不正确的路径导入
我会这样构造您的代码:
常数sbt_sp_add_speed_breaker=[…];
module.exports=sbt\U sp\U add\U speed\U断路器;
//为了保持一致性,最好使用相同的变量名
const sbt_sp_add_speed_breaker=require(“./path/to/module/sbt_sp_add_speed_breaker”);
您访问了错误的变量。执行require的变量名与执行导出的变量名相同。请使用:
var schema = require('./../spschema/index');
var sp = schema.sbt_sp_add_speed_breaker;
如果要使用var sp=schema.sp\u name代码>您应该有:
module.exports.sp_name = sbt_sp_add_speed_breaker;
Sitepoint提供了有关JavaScript模块导出的良好教程:
全解
文件结构
spschema/index.js
src/app.js
您的模块导出不正确。请按如下方式导出
module.exports = sbt_sp_add_speed_breaker
那就试试看,
var schema = require('./../spschema/index');
var sp = schema.sbt_sp_add_speed_breaker;
sbt\u sp\u add\u speed\u breaker
您从spschema/index
导出的是一个数组。因此,如果打印schema.sbt\u sp\u add\u speed\u breaker
。您将得到一个数组
。您需要使用索引来访问,而不是使用
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
};
'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]}`);
index.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
};
'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]}`);
这张照片
第一个常量['id',
“设备密钥”,
“纬度”,
“经度”,
“类型”,
“起始纬度”,
“起始经度”,
“结束纬度”,
“结束经度”]
第二个常量[‘姓’、‘姓’、‘性别’]
第三个常量[‘门牌号’、‘路标号’、‘城市’、‘pincode’]
第一个常量数组的第一种类型中的第一个元素=id
第二个常量数组的第一种类型中的第一个元素=第一个\u名称
第三个常量数组的第一种类型中的第一个元素=门号
如果要将变量导出为不同的名称,请使用不同的名称,或以不同的方式导入。因此,选项一,重命名导出:
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';
您能解释一下如何添加它吗?这是es6模块语法,但与您使用的commonjs格式不同。如果您想使用es6模块(例如,从“/bar”导入foo
)您需要使用像BabelAre这样的transpiler从schema/index.js
导出sp_name
?是的..它显示的是sp_name,但schema.sp_name不起作用您能在上面的代码常量sbt_sp_add_speed_breaker={//code}中发布index.js file的完整部分吗这是index.js文件现在我已经编辑了代码@sridhar当我使用console.log(schema)时它显示数组数据但当我使用console.log(sp)时它显示未定义..我不知道为什么,因为你没有在任何地方定义sp\u name
。你必须有这样一个对象var schema={sp_name:'some value'}
才能执行var foo=schema.sp_name
我正在使用它..var sp=schema.sp_name;是的,但是您在哪里为schema.sp_name
?schema.sp_name意味着schema.sbt_sp_add_speed_breaker,所以它必须从consthere获取数据sp_name意味着sbt_sp_add_speed_breaker。动态调用时,您可以通过执行module.exports.
将导出的变量名更改为您想要的任何名称,然后像schema一样使用它。
您如何访问数组?文件结构是什么?我正在使用模式访问数组。sbt_sp_add_speed_breakerI的代码与上面的代码相同,但我不知道调用schema时为什么会出现未定义的错误。sbt\u sp\u添加\u速度\u断路器未输入if contition@Sridhar@Lllll你能控制.log(模式)吗?是的[“id”,“设备密钥”,“纬度”,“经度”,“类型”,“开始经度”,“开始经度”,“结束经度”,“结束经度]]这是输出耶,正在工作..如果我在同一个文件中使用多个常量并导出它们怎么办..如何使用它们?@llll我已经更新了代码,从一个文件中导出多个常量,并在index.js
中使用它们,您尝试过哪些?什么不起作用?或者最好的事情是,您能在其他模块中做到这一点吗?
:const schema=require('./../spschema/index');console.log(Object.keys(schema)
。这样我们就可以看到您从索引文件中导出了哪些键。[“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”]这是调用console.log(Object.keys(schema)时的输出
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';