错误 TS2339:属性 'coord' 在类型 'Mixed' 上不存在
error TS2339: Property 'coord' does not exist on type 'Mixed'
我正在尝试从 city 对象获取 lat
和 lon
属性,但出现 Typescript 错误
JSON 架构是这样的
{"_id":"vflv511vfsvsv51","name:"dallas","weather":{"coord":{ "lon":"-96.7836","lat": "32.7668"}}}
所以通常要访问它们,我们应该写这样的东西 city.weather.lon
错误
[09:52:31] File change detected. Starting incremental compilation...
src/cities/cities.service.ts:130:30 - error TS2339: Property 'city' does not exist on type 'Mixed'.
130 const lat = city.weather.coord.lat;
~~~~~
src/cities/cities.service.ts:131:30 - error TS2339: Property 'city' does not exist on type 'Mixed'.
131 const lon = city.weather.coord.lon;
~~~~~
[09:52:32] Found 2 errors. Watching for file changes.
cities.service.ts
async GetCityWeather(cityName) {
const city = await this.cityModel.findOne({ name: cityName });
if (!city) {
this.getCityLastWeather(cityName);
}
const lat = city.weather.coord.lat;
const lon = city.weather.coord.lon;
const last7DaysWeather = this.getCityLastXDaysWeather(lat, lon);
...
}
city.model.ts
import * as mongoose from 'mongoose';
export const CitySchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
index: true,
},
weather: mongoose.SchemaTypes.Mixed,
});
export interface City {
id: mongoose.Schema.Types.ObjectId;
name: string;
weather: mongoose.Schema.Types.Mixed;
}
好像是 typescript 的问题而不是 mongoose(虽然我觉得很奇怪 weather
在 ts 中没有定义为 any
),你可以试试这个:
city.weather['coord']['lon']
参考:
城市名称可能 return 如果找不到则为空。为了防止这种情况,我建议您将城市全部降低,并且还 trim 空格以使搜索更准确。
const city = await this.cityModel.findOne({ name: cityName.toLowerCase().trim() });
//save city name in db also as name.toLowerCase().trim()
if (!city) {
this.getCityLastWeather(cityName);
}
const lat = city.weather.coord.lat;
const lon = city.weather.coord.lon;
const last7DaysWeather = this.getCityLastXDaysWeather(lat, lon);
...
}
我正在尝试从 city 对象获取 lat
和 lon
属性,但出现 Typescript 错误
JSON 架构是这样的
{"_id":"vflv511vfsvsv51","name:"dallas","weather":{"coord":{ "lon":"-96.7836","lat": "32.7668"}}}
所以通常要访问它们,我们应该写这样的东西 city.weather.lon
错误
[09:52:31] File change detected. Starting incremental compilation...
src/cities/cities.service.ts:130:30 - error TS2339: Property 'city' does not exist on type 'Mixed'.
130 const lat = city.weather.coord.lat;
~~~~~
src/cities/cities.service.ts:131:30 - error TS2339: Property 'city' does not exist on type 'Mixed'.
131 const lon = city.weather.coord.lon;
~~~~~
[09:52:32] Found 2 errors. Watching for file changes.
cities.service.ts
async GetCityWeather(cityName) {
const city = await this.cityModel.findOne({ name: cityName });
if (!city) {
this.getCityLastWeather(cityName);
}
const lat = city.weather.coord.lat;
const lon = city.weather.coord.lon;
const last7DaysWeather = this.getCityLastXDaysWeather(lat, lon);
...
}
city.model.ts
import * as mongoose from 'mongoose';
export const CitySchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
index: true,
},
weather: mongoose.SchemaTypes.Mixed,
});
export interface City {
id: mongoose.Schema.Types.ObjectId;
name: string;
weather: mongoose.Schema.Types.Mixed;
}
好像是 typescript 的问题而不是 mongoose(虽然我觉得很奇怪 weather
在 ts 中没有定义为 any
),你可以试试这个:
city.weather['coord']['lon']
参考:
城市名称可能 return 如果找不到则为空。为了防止这种情况,我建议您将城市全部降低,并且还 trim 空格以使搜索更准确。
const city = await this.cityModel.findOne({ name: cityName.toLowerCase().trim() });
//save city name in db also as name.toLowerCase().trim()
if (!city) {
this.getCityLastWeather(cityName);
}
const lat = city.weather.coord.lat;
const lon = city.weather.coord.lon;
const last7DaysWeather = this.getCityLastXDaysWeather(lat, lon);
...
}