1

I have a express.js route whose code is below: -

const path = require("path");
const express = require("express");
const hbs = require("hbs");
const weather = require("./weather");

const app = express();

app.get("/weather", (req, res) => {
  if (!req.query.city) {
    return res.send({
      error: "City Not Found",
    });
  }
  res.send({
    currentTemp: weather.temp,
  });
});

And I also have a file to fetch data from api using axios whose code is here

const axios = require("axios");

axios
  .get(
    "https://api.openweathermap.org/data/2.5/weather?q=samalkha&appid=91645b79f9eac8808153c90472150f2d"
  )
  .then(function (response) {
    module.exports = {
        temp: response.data.main.temp
    }
  })
  .catch(function (error) {
    console.log("Error Spotter");
  });

As I am using res.send I should get a json with currentTemp and the value of current temp should be temperature that I will get from weather.js file but I am getting a blank json array.

1
  • Did you check what is the result of currentTemp: weather.temp ? Is that giving you the data Also try to include res.json({currentTemp: weather.temp,}) and see Commented Aug 2, 2021 at 15:00

3 Answers 3

1

Try this.

You'll get the temperature in the localhost:3000

If you want to render the data for EJS (or something) instead of .then((data) => res.json(data.main.temp)) use:

.then((data) => res.render("index", { weather: data })

--

const URL =
  "https://api.openweathermap.org/data/2.5/weather?q=samalkha&appid=91645b79f9eac8808153c90472150f2d"

const express = require("express")
const axios = require("axios")
const app = express()
const PORT = 3000

app.get("/", (req, res) => {
  axios
    .get(URL)
    .then((response) => response.data)
    .then((data) => res.json(data.main.temp))
    .catch((err) => console.log(err))
})

app.listen(PORT, () => {
  console.log(`Listening at http://localhost:${PORT}`)
})
Sign up to request clarification or add additional context in comments.

Comments

0

module.exports is processed when the module is being defined. when you import weather, it does not exists therefore you get no data.

try to export a function which does the request and add a callback function as argument so you can pass the request result to it.

Comments

0

Change your weather.js to the following example:

const axios = require("axios");
let temperature;

async function getTemp() {
    await axios
        .get("https://api.openweathermap.org/data/2.5/weather?q=samalkha&appid=91645b79f9eac8808153c90472150f2d")
        .then(function (response) {
            temperature = response.data.main.temp
        })
        .catch(function (error) {
            console.log("Error Spotter");
        });
}
module.exports = {
    temp: getTemp
}

This will actually return the fetched temperature.

Comments

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.