I'm new to mongoose, and I'm trying to create an app that uses the OpenWeatherMap API. After requesting the data form the API, I save them to my MongoDB and then I want to return the results as a json, so I call the following function:
async function saveForecast(data) {
// Code here for creating the "forecastList" from the data and fetching the "savedLocation" from the DB
const newForecast = new Forecast({
_id: new mongoose.Types.ObjectId(),
location: savedLocation,
city: {
id: data.city.id,
name: data.city.name,
coordinates: data.city.coord,
country: data.city.country
},
forecasts: forecastList
});
try {
const savedForecast = await newForecast.save();
return savedForecast.populate('location').lean().exec(); //FIXME: The lean() here throws TypeError: savedForecast.populate(...).lean is not a function
} catch (err) {
console.log('Error while saving forecast. ' + err);
}
}
The "newForecast" is being saved successfully in the DB, however when I try adding the .lean() after my populate I get the following error:
TypeError: savedForecast.populate(...).lean is not a function
I've used lean() on find queries and it works fine, but I can't get it to work with my "newForecast" object, even though the "savedForecast" is a mongoose document, as the debugger shows me.
Any ideas why lean() is not working? Thank you!