I am trying to create a city then making a request to OpenWeatherMap api to get the weather info of this city then store it into DB (MongoDB Atlas).
But I cannot a proper schema type for the weather field, I tried to use 'Mixed' but it's not recognized
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;
// Get city weather 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
[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)
CodePudding user response:
I used this piece of code and it seems to be working
import * as mongoose from 'mongoose';
export const CitySchema = new mongoose.Schema({
name: String,
weather: mongoose.SchemaTypes.Mixed,
});
export interface City {
id: number;
name: string;
weather: mongoose.Mixed;
}
But if you have any feedback or a better solution I'm all ears