错误 [ExceptionsHandler] 城市验证失败:天气:转换为字符串失败

ERROR [ExceptionsHandler] City validation failed: weather: Cast to string failed

我正在尝试创建一个城市,然后向 OpenWeatherMap api 发出请求以获取该城市的天气信息,然后将其存储到数据库(MongoDB Atlas)中。
但是我无法为天气字段提供合适的模式类型,我尝试使用 'Mixed' 但无法识别

结构

|- 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 {}

错误

[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)

我使用了这段代码,它似乎可以工作

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;
}

但如果您有任何反馈或更好的解决方案,我会洗耳恭听