I have following node function for api server:
router.post('/find', async (req, res) => {
try {
const firewalls = [];
let count = 0;
const devices = await Device.find({ ...req.body });
devices.forEach(async (item) => {
const toObject = item.toJSON();
const ping = await checkPing(integerToIp(toObject.ipAddress));
console.log(ping);
count++;
firewalls.push({
...toObject,
status: ping,
});
});
if (count === devices.length) {
res.json({ success: true, data: firewalls });
}
} catch (error) {}
});
and this function:
const ping = require('ping');
function checkPing(ipAddress) {
return new Promise(async (resolve, reject) => {
let result = {};
if (ipAddress) {
const res = await ping.promise.probe(ipAddress);
result['alive'] = res.alive;
}
return resolve(result);
});
}
in router.post
i will get firewalls array as expected, but if ii want to add status property with result from checkPing function it wont save it. it will console.log just before but not in status property.
Iam sure iam missing something obvious but cant figure it out. Any help please?