簡體   English   中英

如何將此變量從類方法導出到另一個文件?

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

  1. 請轉到第60行或此代碼的“ for循環內的變量名Amit”,我希望將該變量名導出到另一個文件中,但是我不能使用tat變量值到另一個文件。 當我通過要求它顯示未定義來做到這一點時。

     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. 項目清單

您可以在您的類中有一個變量,並在您的服務函數中為其分配一個值

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;

然后在另一個要導入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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM