Ad

How Do I Check If Hits Has A Value Or Not?

- 1 answer

My goal is to send an alarm if the [hits] field is empty.

This is my result:

hits: {
 total: { value: 10000, relation: 'gte' },
 max_score: 1,
 hits: [
   [Object], [Object],
   [Object], [Object],
   [Object], [Object],
   [Object], [Object],
   [Object], [Object]
 ]
 }
}

and this is my code:

const axios = require('axios');
const query = {
query: {
match: {
 "kubernetes.pod.name.keyword" : "fcppaddy-596b798c77-9hwbh"
      }
     }
    };
 axios.get("https://tommaso.fachin:[email protected]:9200/filebeat-7.15.2/_search? 
 pretty", query)
  .then((res) => {
    console.log(res.data);
    console.log(res.status);
console.log(JSON.stringify(res.data));
  });
Ad

Answer

I'm still not 100%, but from your comments it seems like you want to check if hits has anything in it, and if so do something for each one:

if(hits.length > 0) {
  hits.forEach((hit) => {
    triggerAlarm()
  })
}

So obviously you can choose what to send as a param to triggerAlarm() but I think this is what you're asking for

Edit, based on your comment:

if(hits.hits.length > 0) {
  hits.forEach((hit) => {
    // for each item in hits.hits array, send mail
    ModuloMail()
  })
}
Ad
source: stackoverflow.com
Ad