0

I am trying to fetch data from an api from different endpoints(ip adresses) using await statement. It seems to work for some but not others. However, when I use "then" (the commented code" statements it works for all.

The purpose is to save a .txt log file. The result is the same on the saved .txt file and on the console logging as well

Any help is appreciated. Thanks!

Here is my code that's fetching and logging data.

class Status {
    async doFetch(api, ep) {
          // api.status.get('SystemUnit ProductId')
    //     .then(res => {
    //         console.log(res)
    //         this.acc += res + '\r\n'
    //     }).then( () => {
    //         api.status.get('UserInterface ContactInfo Name')
    //             .then(name => {
    //                 console.log(name)
    //                 this.acc += name + '\r\n';
    //             })
    //     })
        function timeout(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        }

        const PRODUCT_ID = await api.status.get('SystemUnit ProductId');
        const SYSTEM_NAME = await api.status.get('UserInterface ContactInfo Name');
        const SOFTWARE_VERSION = await api.status.get('SystemUnit Software Version');
        const MAC_ADDRESS = await api.status.get('Network 1 Ethernet MacAddress');
        const TEMPERATURE = await api.status.get('SystemUnit Hardware Temperature');
        const SIP_Registration_Status = await api.status.get('SIP Registration 1 Status');
        const SIP_Proxy_Address = await api.status.get('SIP Proxy 1 Address');
        const SIP_Registration_URI = await api.status.get('SIP Registration 1 URI');

 await timeout(500);

        return`Product ID: ${await PRODUCT_ID}\r\n
System name: ${await SYSTEM_NAME}\r\n
IP address: ${ep}\r\n
Software version: ${await SOFTWARE_VERSION}\r\n
MAC address: ${await MAC_ADDRESS}\r\n
Temperature: ${await TEMPERATURE}˚\r\n
Sip registration status: ${await SIP_Registration_Status}\r\n
Sip proxy address: ${await SIP_Proxy_Address}\r\n
Sip registration URL: ${await SIP_Registration_URI}\r\n\r\n
`;        
    }
}

(function() {
    const server = new Server(1000);
    logs.config(FILE_NAME, DIR);
    let api;
    let ep;
    const toRun = (body) => {
        api = body.api;
        ep = body.endpoint
        const s = new Status();
        if (ep !== '' && ep !== undefined)
            s.doFetch(api, ep)
                .then(acc => {
                    console.log(acc)
                    logs.buildString(acc);
                })
                .catch(err => {
                    if (err.code !== 3)
                        console.error(err);
                });
    }
    server.runCommand(toRun, logs.save)
})()
Steven
  • 35
  • 5
  • If you have code, please explain what you expect it to do as well as what you're seeing it do. As presented, this code looks fine. – Mike 'Pomax' Kamermans Jun 19 '20 at 04:00
  • thanks, basically just want to get results of PRODUCT_ID, SYSTEM_NAME, SOFTWARE_VERSION, etc and save it. I currently have 4 endpoints that I am looping over, I am getting what I expect for two of them but I get nothing ("") for the other two. – Steven Jun 19 '20 at 04:05
  • Maybe check the implementation of "the other two"? – hangindev.com Jun 19 '20 at 04:10
  • Please explain things _in your post_. Comments for more details are never asked purely for the benefit of the person asking, they're to get you to update your post [with all the details that should be in it](/help/how-to-ask) so that everyone else has all the information they need to help you without going through a comment thread. – Mike 'Pomax' Kamermans Jun 19 '20 at 14:50
  • It turns out that for certain endpoints, the command to retrieve the temperature value is not supported and just returns an empty string entirely `const TEMPERATURE = await api.status.get('SystemUnit Hardware Temperature');` throws an error and ignores the rest of the await statements. However, I still don't understand why it doesn't return the rest of the values. – Steven Jun 22 '20 at 03:23
  • [Use `Promise.all` instead of multiple awaits after promise creations](https://stackoverflow.com/questions/46889290/waiting-for-more-than-one-concurrent-await-operation). – Bergi Jun 22 '20 at 06:32
  • Yes that would make more sense. Thanks! – Steven Jun 23 '20 at 21:13

1 Answers1

0

I tried this to get around it:

`

        let PRODUCT_ID;
        let SYSTEM_NAME;
        let SOFTWARE_VERSION;
        let MAC_ADDRESS;
        let SIP_Registration_Status;
        let SIP_Proxy_Address;
        let SIP_Registration_URI;
        let TEMPERATURE;

        try {                       
            PRODUCT_ID = await api.status.get('SystemUnit ProductId');
            SYSTEM_NAME = await api.status.get('UserInterface ContactInfo Name');
            SOFTWARE_VERSION = await api.status.get('SystemUnit Software Version');
            MAC_ADDRESS = await api.status.get('Network 1 Ethernet MacAddress');                        
            SIP_Registration_Status = await api.status.get('SIP Registration 1 Status');
            SIP_Proxy_Address = await api.status.get('SIP Proxy 1 Address');
            SIP_Registration_URI = await api.status.get('SIP Registration 1 URI');
            TEMPERATURE = await api.status.get('SystemUnit Hardware Temperature');            
        } catch (err) {            
            if (err.code == 3 || err.data.xpath === 'Status/SystemUnit/Hardware/Temperature') {
//THIS WORKS FOR THE REST OF ENDPOINTS IF THE FIRST ONE FAILS
                TEMPERATURE = await api.status.get('SystemUnit Hardware Monitoring Temperature Status');                
            } else console.error(err);
        }
        finally {
            timeout(500);
            return`
Product ID: ${PRODUCT_ID}\r\n
System name: ${SYSTEM_NAME}\r\n
IP address: ${ep}\r\n
Software version: ${SOFTWARE_VERSION}\r\n
MAC address: ${MAC_ADDRESS}\r\n
Sip registration status: ${SIP_Registration_Status}\r\n
Sip proxy address: ${SIP_Proxy_Address}\r\n
Sip registration URL: ${SIP_Registration_URI}\r\n
Temperature: ${TEMPERATURE}\r\n\r\n
`;
        }
    

`

Steven
  • 35
  • 5