0

Im trying to implement a Network discovery search which returns all devices that are linked with my local network. My code so far looks like this:

const find = require('local-devices');

// Find all local network devices.
find().then(devices => {
  console.log(devices);
})

And the returned array in the console looks like this:

[
  { name: '?', ip: '192.168.0.174', mac: '00:17:88:65:f4:4d' },
  { name: '?', ip: '192.168.0.222', mac: '80:be:05:73:bc:g5' },
  { name: '?', ip: '192.168.0.190', mac: '0c:fe:45:4d:b8:28' }
]

So, I know the Mac Adress of the device im looking for already, my problem is, how can I search for the mac adress in the Array, and return the specific ip Adress of this device? For example, I pass the mac adress "00:17:88:65:f4:4d" into a function, and the function should return "192.168.0.174" which I can save in a variable and work with in the following code.

I might be able to solve this with the find() function, but how can I tell the code, that I only want the ip adress, and not the full element of the array saved? I thought about something like:

const ip = devices.find('00:17:88:65:f4:4d'); 

But this would only return the full element, if the syntax was correct.

4
  • 1
    You can't really do what you're asking. The package you're using is asynchronous. You can identify the device inside the .then() callback, but you can't wrap that in a function that'd synchronously let you fetch a particular IP address. Commented Jun 2, 2020 at 12:56
  • Now i understand thanks Pointy i removed my code Commented Jun 2, 2020 at 13:02
  • Ah i see the problem. Since I dont have the Array filled with all ip adresses right away, and it takes a bit of time until it is filled, I cant instantly try to find an ip adress out of this array? Is there something like a "wait" function, which gives the code 5 seconds until it tries to do the following commands? Commented Jun 2, 2020 at 13:18
  • You cannot make an asynchronous API into a syncrhonous API. There is no "wait" or "sleep" in JavaScript. You have to structure your code to embrace the asynchronous nature of the language. Commented Jun 2, 2020 at 14:25

4 Answers 4

1

if the syntax was correct

Well, your first step would be to fix that. Perhaps:

const element = devices.find(d => d.mac === '00:17:88:65:f4:4d');

Now you should have the element you're looking for (or null).

this would only return the full element

If you only want one property of that element, return that property:

return element ? element.ip : '';

Here I arbitrarily picked an empty string as a default return value if a matching element is not found. You can replace that with any default you'd like, or leave the return value undefined.


Note that there's two instances of find here, which may cause confusion. The imported find is called on its own and returns a Promise. And in the callback for that Promise is where we use .find on an array:

const find = require('local-devices');

find().then(devices => {
  const element = devices.find(d => d.mac === '00:17:88:65:f4:4d');
  console.log(element ? element.ip : '');
});

Whether you return from the promise callback and use that resolved promise elsewhere, or use the resulting value directly in this callback, is up to your needs. A single function won't be able to enclose the whole operation and synchronously return the value you want, but that function can return the promise which resolve to the value or can accept a callback to invoke when the promise resolves.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm pretty sure that that package is asynchronous, and that the find() function returns a Promise.
@Pointy: Isn't this particular .find() on the array returned by the first Promise? It looks like there's two finds involved here.
0
var devices = [
  { name: '?', ip: '192.168.0.174', mac: '00:17:88:65:f4:4d' },
  { name: '?', ip: '192.168.0.222', mac: '80:be:05:73:bc:g5' },
  { name: '?', ip: '192.168.0.190', mac: '0c:fe:45:4d:b8:28' }
];

const getIpFromMacAddress = macAddress => {
  for (let i = 0; i < devices.length; i++) {
    if (devices[i].mac === macAddress){
      return devices[i].ip
    }
  }
  return 'device not found'
}

getIpFromMacAddress('00:17:88:65:f4:4d') // "192.168.0.174"

Comments

0

Sorry, I was just replying with this answer but noticed Wais has already given the same answer above! here is an example using .forEach rather than a for loop though

let details = [
  { name: '?', ip: '192.168.0.174', mac: '00:17:88:65:f4:4d' },
  { name: '?', ip: '192.168.0.222', mac: '80:be:05:73:bc:g5' },
  { name: '?', ip: '192.168.0.190', mac: '0c:fe:45:4d:b8:28' }
]

details.forEach((item, index) => {
  if(item.mac === "80:be:05:73:bc:g5") {
    return item.ip;
  } 
});

Comments

0

Here is how you can do it in the classic way:

var devices = [
  { name: '?', ip: '192.168.0.174', mac: '00:17:88:65:f4:4d' },
  { name: '?', ip: '192.168.0.222', mac: '80:be:05:73:bc:g5' },
  { name: '?', ip: '192.168.0.190', mac: '0c:fe:45:4d:b8:28' }
]

for(var i = 0; i < devices.length; i++) {
  if(devices[i].mac == "00:17:88:65:f4:4d") {
    return devices[i].ip;
  }
}

1 Comment

careful not to use the assignment operator - should use === instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.