Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

71
Views
react showing 2 same outcome

i am trying to send a prompt a notification when my sensor hit the threshold. but instead of 1 prompt, i receive 2 of the same prompt.

const TempGraph = () => {

 const [data, setData] = useState([]); // sound , temp, humidity , light , motion .
 const [thresholddata,setThresholdData] = useState([]);
 var result = [];
//  var i = 0;
 //const thresholds = '35';
 function addZero(i) {
  if (i < 10) {i = "0" + i}
  return i;
}

  useEffect(() => {
    const interval = setInterval(() => asyncFetch(),5000) //5000ms = 5sec

    return () => clearInterval(interval) // clear the interval everytime
  }, []);
  /* fetch specific sensor type */ 
  const asyncFetch = async() => {
      await fetch('api')
      .then((response) => response.json())
      .then((json) => setData(json))
      .catch((error) => {
        console.log('fetch data failed', error);
      });
      
    //fetching threshold data
     await fetch('api')
     .then((response) => response.json())
     .then((json)=>setThresholdData(json))
     .catch((error) => {
       console.log('fetch threshold data failed',error);
     });
     
  };

  if(data.length != 0){
      /* Loop through threshold table to get the same type id */
      for(var x =0;x<thresholddata.length;x++){
        // console.log(thresholddata[i])
          if(thresholddata[x].typeid == data[data.length-1].typeid && thresholddata[x].sensorid == data[data.length-1].sensorid){
          result = thresholddata[x];
            console.log(result)
  
        }
      }
      /* notification when the data value is over the threshold */
      if(result.upperthreshold < data[data.length-1].value){
        openNotificationWithIcon('warning',data[data.length-1].location);
        
      }

  }
   /* graph here */
}

I used a for loop to loop through all the data and make sure the id of them are the same. and when the value of the sensors hits above the threshold it will go to the notification. but the result variable saved 2 same value.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!