Home > Blockchain >  I'm unable to send a response to my react.js using http.get in node
I'm unable to send a response to my react.js using http.get in node

Time:10-26

I'm trying to get the temperature data from my node.js backend sent to react.js but i kept getting res.send is not a funtion

Sample code here

app.get("/gettemperature", (req, res) => {
    const email = req.query.email;

    let stmt = `SELECT * FROM users WHERE email=?`;
    let todo = [email];
    
    db.query(stmt, todo, (err, results, fields) => {
      if (err) {
        console.error(err.message);
      }
      
      if(results.length > 0 ){
          let id = results[0].id;

          let getID = `SELECT * FROM controlModules WHERE deviceowner=?`;
          let getidData = [id];

          db.query(getID, getidData, (err, resulta, fields) => {
            if (err) {
              console.error(err.message);
            }

            if(resulta.length > 0){
              
              let lanip = resulta[0].ipaddress;

              let url = "http://" lanip "/data";

              http.get(url,(res) => {
                  let body = "";
          
                  res.on("data", (chunk) => {
                      body  = chunk;
                  });
          
                  res.on("end", () => {
                      try {
                        let json = JSON.parse(body);
                        const temp_actual = json.temperature.value;
                        console.log(temp_actual);
                        
                        res.setHeader('Content-Type', 'application/json');
                        res.end(
                          JSON.stringify({
                            value: temp_actual
                          })
                        );
                      } catch (error) {
                          console.error(error.message);
                      };
                  });
          
              }).on("error", (error) => {
                  console.error(error.message);
              });
            }
          });
      }

    });

  });

i really need to return/send/respond the temperature data to my front end but i'm getting said error, is there a different way to return data?

CodePudding user response:

It looks like you are mixing up an HTTP server you wrote in Node (although you haven't shown any relevant code) and an HTTP client you also wrote in Node.

res is an argument received by the callback you pass to http.get and contains data about the response received by your HTTP client.

Meanwhile, somewhere else (not shown) you have a different variable also called res which is the object your HTTP server uses to send its response to the browser running your React code.

You are calling res.send and wanting res to be the latter but it is really the former.

Since you haven't shown us the HTTP server code, it is hard to say where that res is, but there is a good chance you have shadowed it and can solve your problem by using different names (e.g. client_res and server_res).


That said. I strongly recommend avoiding using the http module directly as the API follows out of date design patterns and isn't very friendly. Consider using fetch or axios for making HTTP requests and Express.js for writing HTTP servers.

  • Related