[英]How do I export this variable from class method to another file?
Please go to line 60 or (Variable name Amit inside the for loop) of this of this code, i want that variable name to exports in another file, but i cannot use tat vairiable value to other file. 请转到第60行或此代码的“ for循环内的变量名Amit”,我希望将该变量名导出到另一个文件中,但是我不能使用tat变量值到另一个文件。 when i do it by requiring it shows undefined. 当我通过要求它显示未定义来做到这一点时。
class FetchData { constructor(device_id, header) { this.device_id = device_id; this.header= header; } services() { // Get Device Details by deviceId return httpRequest({ method : 'get', json : true, url : `${config.url.usermgmt_api}/device/${this.device_id}`, headers : { 'x-access-token': this.header } }).then( response => { if(response.hasOwnProperty('auth') && !response.auth) { throw new Error(response.message); } let thingGreenLinks = []; // Current Active TG into the Array if(response.hasOwnProperty('mapping')) { //We have to scroll through the mapping and return the shortcode and thingid and ports to connect thingGreenLinks.push(response.mapping[0].TG_Port.split(/_/)[0]); } else { throw (response.message)? response.message : 'No mapping found. Device ID: '+ deviceid; } // Check mapping history if(response.hasOwnProperty('map_history') && response.map_history.length > 0) { response.map_history.reverse().map( record => { thingGreenLinks.push(record.mapping[0].TG_Port.split(/_/)[0]) }) } return thingGreenLinks; }).then(async tg_ids => { logger.info('Fetching Hardware IDs of ThingGreen ...'); let Hardware_IDs = []; for (let i = 0; i <= tg_ids.length - 1; i++) { await fetchHardwareID(tg_ids[i], this.header) .then( response => { Hardware_IDs.push(response.HardwareId) }) let Amit = 'amit' module.exports = { amit : Amit} } logger.info('Received Following Hardware IDs :\\n'+Hardware_IDs.join('\\n')); return Hardware_IDs; }) .catch( err => Promise.reject(new Error(err.message))); } } exports.FetchData = FetchData;
you can have a variable in your class and assign a value to it inside your services function 您可以在您的类中有一个变量,并在您的服务函数中为其分配一个值
class FetchData {
constructor(device_id, header) {
this.device_id = device_id;
this.header= header;
this.amit = "";
}
services() {
//your sevices code
this.amit='amit'
}
} exports.FetchData = FetchData;
then in another file where you are importing FetchData,you can access it with object.varible name. 然后在另一个要导入FetchData的文件中,可以使用object.varible名称访问它。
var fetchDataObject = new FetchData(device_id, header);
fetchDataObject.amit //this is your variable amit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.