My goal is to develop a city and then use the OpenWeatherMap API to fetch the weather information for that city, which will be stored in a MongoDB Atlas database.
However, I am facing an issue with defining the correct schema type for the weather field. Although I attempted to use 'Mixed', it seems to be unrecognized.
Project Structure
|- cities
|- | city.model.ts
|- | cities.controller.ts
|- | cities.service.ts
city.model.ts
import * as mongoose from 'mongoose';
export const CitySchema = new mongoose.Schema({
name: String,
weather: String,
});
export interface City {
id: number;
name: string;
weather: mongoose.Mixed;
}
cities.controller.ts
@Controller('cities')
export class CitiesController {
constructor(private readonly citiesService: CitiesService) {}
@Post()
createCity(@Body() createCityDto: CreateCityDto) {
return this.citiesService.createCity(createCityDto);
}
}
cities.service.ts
@Injectable()
export class CitiesService {
constructor(
@InjectModel('City') private readonly cityModel: Model<City>,
private readonly httpService: HttpService
) {}
async createCity(createCityDto: CreateCityDto) {
const { name } = createCityDto;
// Fetching city weather data from OpenWeatherMap
const weather = this.httpService
.get(
`api.openweathermap.org/data/2.5/weather?q=${name}&appid=${process.env.OPEN_WEATHER_API_KEY}`,
)
.pipe(map((response) => response.data));
const city = new this.cityModel({
name,
weather,
});
await city.save();
return city;
}
}
cities.module.ts
...
@Module({
imports: [
HttpModule,
MongooseModule.forFeature([{ name: 'City', schema: CitySchema }]),
],
controllers: [CitiesController],
providers: [CitiesService],
})
export class CitiesModule {}
Error Log
[Nest] 12852 - 22/12/2021, 10:35:21 LOG [NestApplication] Nest application successfully started +7ms
[Nest] 12852 - 22/12/2021, 10:35:27 ERROR [ExceptionsHandler] City validation failed: weather: Cast to string failed for value "Observable {
source: Observable { _subscribe: [Function (anonymous)] },
operator: [Function (anonymous)]
}" (type Observable) at path "weather"
ValidationError: City validation failed: weather: Cast to string failed for value "Observable {
source: Observable { _subscribe: [Function (anonymous)] },
operator: [Function (anonymous)]
}" (type Observable) at path "weather"
at model.Document.invalidate (C:\Users\ouss\Desktop\coffeeit-assessment\node_modules\mongoose\lib\document.js:2921:32)
at model.$set (C:\Users\ouss\Desktop\coffeeit-assessment\node_modules\mongoose\lib\document.js:1458:12)
at model.$set (C:\Users\ouss\Desktop\coffeeit-assessment\node_modules\mongoose\lib\document.js:1153:16)
at model.Document (C:\Users\ouss\Desktop\coffeeit-assessment\node_modules\mongoose\lib\document.js:158:12)
at model.Model (C:\Users\ouss\Desktop\coffeeit-assessment\node_modules\mongoose\lib\model.js:107:12)
at new model (C:\Users\ouss\Desktop\coffeeit-assessment\node_modules\mongoose\lib\model.js:4777:15)
at CitiesService.createCity (C:\Users\ouss\Desktop\coffeeit-assessment\src\cities\cities.service.ts:29:18)
at CitiesController.createCity (C:\Users\ouss\Desktop\coffeeit-assessment\src\cities\cities.controller.ts:11:31)
at C:\Users\ouss\Desktop\coffeeit-assessment\node_modules\@nestjs\core\router\router-execution-context.js:38:29
at processTicksAndRejections (internal/process/task_queues.js:95:5)