简体   繁体   English

如何将此变量从类方法导出到另一个文件?

[英]How do I export this variable from class method to another file?

  1. 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; 
    1. List item 项目清单

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.

相关问题 如何为包含另一个类/文件中的全局变量的方法编写茉莉花测试? - How do I write a jasmine test for a method that contains a global variable from another class/file? NODEJS,我如何正确地将我的容器 class 的这个方法导出到我管理 express 的另一个 JS 文件? - NODEJS, how do I correctly export this method of my Container class to another JS file where I manage express? 如何从一个文件导出变量,并在另一个文件上require()导出的变量? - How can I export a variable from one file and require() the exported variable on another file? 如何将变量导出到另一个文件? - How to Export a Variable to Another file? 如何将类从一个文件导出到Node中的另一个文件? - How can I export a class from one file to another file in Node? 如何从另一个文件更改函数中的变量? - How do I change variable in a function from another file? 如何将变量从一个 javascript 文件传递到另一个文件? - How do I pass a variable from one javascript file to another? 如何从另一个 html 文件导入变量? (我使用导入/导出,但出现错误) - How to import variable from another html file? (I use import/export, but error occurs) 在javascript中,如何从同一个类中的另一个方法调用类方法? - In javascript, how do I call a class method from another method in the same class? 如何将字符串导出到另一个Vue文件? - How do I export a string to another Vue file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM