How to save Weekly Weather Forecast data in MongoDB using the Mongoose model with weather API?

92 Views Asked by At

How to save Weekly Weather Forecast data in MongoDB using the Mongoose model?

I'm a beginner and as my first mini-project, I'm writing an app for getting weekly weather forecast data from OpenWeatherApi. The issue I'm facing is that the Api I'm using returns the weather forecast for 7 days every 3 hours. the Json file would be like this:

"list": [
        {
            "dt": 1684746000,
            "main": {
                "temp": 300.84,
                "feels_like": 300.03,
                "temp_min": 300.84,
                "temp_max": 301.69,
                "pressure": 1021,
                "sea_level": 1021,
                "grnd_level": 853,
                "humidity": 30,
                "temp_kf": -0.85
            },
            "weather": [
                {
                    "id": 803,
                    "main": "Clouds",
                    "description": "broken clouds",
                    "icon": "04d"
                }
            ],
            "clouds": {
                "all": 75
            },
            "wind": {
                "speed": 0.66,
                "deg": 278,
                "gust": 2.33
            },
            "visibility": 10000,
            "pop": 0,
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2023-05-22 09:00:00"
        },

in the dt_text part as you can see I have the date + the hour. the json file includes 40 results like the above for every 3 hours.

I want to save this data in my Database. The mongoose model I wrote is as below:

const dailyWeatherSchema = new mongoose.Schema({
  dt: String,
  dt_text: String,
  city: {
    type: String,
    name: String,
    country: String,
    unique: true,
  },
  main: {
    temp: Number,
    feels_like: Number,
    temp_min: Number,
    temp_max: Number,
    humidity: Number,
  },
  weather: [
    {
      main: String,
      description: String,
    },
  ],
});

const weeklyWeatherSchema = new mongoose.Schema({
  weekStartDate: { type: Date, required: true },
  monday: dailyWeatherSchema,
  tuesday: dailyWeatherSchema,
  wednesday: dailyWeatherSchema,
  thursday: dailyWeatherSchema,
  friday: dailyWeatherSchema,
  saturday: dailyWeatherSchema,
  sunday: dailyWeatherSchema,
});

const WeeklyWeather = mongoose.model(
  'Weekly Weather',
  weeklyWeatherSchema
);

module.exports = WeeklyWeather

I want to save data for each day only at 09:00 am but I got stuck in the first part. the data won't save in my dataBase.

my controller is as belowe:

app.get('/weatherg/:city', async (req, res) => {
  let city = req.params.city;
  console.log(city);

  await axios
    .get(
      `https://api.openweathermap.org/data/2.5/forecast?q=${city}&&appid=${API_KEY}`
    )
    .then(async (response) => {
      const newForcastData = await response.data;

      console.log(newForcastData);
      await new Forcast(newForcastData).save();
    });

I got the data from "newForcastData" part but the data won't save in database. I get the below err:

   this.$__.validationError = new ValidationError(this);
                               ^

ValidationError: Weekly Weather validation failed: weekStartDate: Path `weekStartDate` is required.

So Here are my questions in summary:

  1. How to filter the data only to show data that have the 09:00 part?
  2. Why I get the err with my Mongoose model :(
  3. how to change the "dt" part so the data will save with day name in dataBase.

Thank you

0

There are 0 best solutions below