Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

566
Views
error TS2339: la propiedad 'coord' no existe en el tipo 'Mixto'

Estoy tratando de obtener las propiedades de lat y lon del objeto de la ciudad, pero aparece un error de mecanografiado

El esquema JSON es así {"_id":"vflv511vfsvsv51","name:"dallas","weather":{"coord":{ "lon":"-96.7836","lat": "32.7668"}}} por lo que normalmente para acceder a ellos deberíamos escribir algo como esto city.weather.lon

Error

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

ciudades.servicio.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); ... }

ciudad.modelo.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; }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Parece un problema mecanografiado en lugar de mangosta (aunque me parece extraño que el weather no esté definido como any en ts), puedes probar esto:

 city.weather['coord']['lon']

Referencia: error TS2339: La propiedad 'x' no existe en el tipo 'Y'

over 4 years ago · Santiago Trujillo Report

0

es probable que el nombre de la ciudad devuelva un valor nulo si no se encuentra. Para protegerse contra eso, le sugiero que haga la ciudad más baja y también recorte los espacios para que la búsqueda sea más precisa.

 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); ... }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!