简体   繁体   中英

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. 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.

    var fetchDataObject = new FetchData(device_id, header);
     fetchDataObject.amit //this is your variable amit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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