StephanGeorg / node-dig-dns

Use dig command (domain information grope) for DNS queries in node
MIT License
37 stars 21 forks source link

TypeError: Cannot read property 'replace' of undefined #4

Closed adamramadhan closed 5 years ago

adamramadhan commented 5 years ago

esult.time = Number(data[data.length - 6].replace(';; Query time: ', '').replace(' msec', '')); ^ TypeError: Cannot read property 'replace' of undefined

using a simple script

const dig = require('node-dig-dns');
dig(['google.com', 'ANY'])
  .then((result) => {
    console.log(result)
  })
  .catch((err) => {
    console.log('Error:', err);
  });

anyone have the same problem?

StephanGeorg commented 5 years ago

What exactly do you want to do with that line?

Number(data[data.length - 6].replace(';; Query time: ', '').replace(' msec', ''));
jalamprea commented 5 years ago

I got the same issue. When dig command internally generate a timeout or the final host is not accesible. When DIG return a timeout message it will not throw any error, just a normal response like this: ; <<>> DiG 9.12.2-P1 <<>> @ns50162.webempresa.eu. A fit3d.me ; (1 server found) ;; global options: +cmd ;; connection timed out; no servers could be reached

In your code, in the index.js in line 45 you always try to read data[data.length - 6] that position doesn't exist when DIG return a timeout message or any other issue. I think you should add a try catch, or validate the length of the data array. If the valid conditions are not met, you should throw an error and then reject the promise.