Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
findOne() is returning the whole model instead of a document

I am using findOne() to extract a document. On printing the returned value its prints the correct result but on looping over it, it prints the model instead of the original document. The document in the City table is stored as -

{
 _id: 62e135519567726de42421c2,
configType: 'cityConfig'
'16': {
        cityName: 'Delhi',
        metro: true,
        tier1: true
      },
'17': {
        cityName: 'Indore',
        metro: false,
        tier1: false
      }
}

The code -

const getCities = async () => {
  const cityConfig = await City.findOne({
    configType: "cityConfig"
  });

  console.log("cityConfig - ", cityConfig);
  
  let metroCities = [];

  for(var city in cityConfig) {
    console.log("city - ", city);
    if(cityConfig[city].metro == true) { // throws an error here obviously
      metroCities.push(city);
    }
  }
  return metroCities;

};

The ouptut of cityConfig is correct, but inside the for loop, its iterating over the model instead of the document. Output -

cityPassConfig -  {
 '16': {
    cityName: 'Nagpur',
    passIssuedCities: false,
    digitalDiscountEnabledCities: false,
    bhopalIndore: false,
    mPanelCitiesForPass: false
  },
  '17': {
    cityName: 'Mumbai',
    passIssuedCities: false,
    digitalDiscountEnabledCities: false,
    bhopalIndore: false,
    mPanelCitiesForPass: false
  },
 _id: 62e288807b59432f87e32a82,
  configType: 'cityPassConfig'
}
city -  $__
city -  isNew
city -  errors
city -  $locals
city -  $op
city -  _doc
city -  $init
city -  db
city -  discriminators
city -  configType
city -  cityId
city -  configData
city -  enabled
city -  lastModifiedby
city -  _id
city -  updatedAt
city -  createdAt
.
.
.
.

I want it to just iterate over 16 and 17 (the keys of the object). How to do that? Node version - v14.15.4

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The issue seems to be with your document:

{
    _id: 62e135519567726de42421c2,
    configType: 'cityConfig' // There should be a comma here, but not sure if that's affecting anything.
    '16': {
        cityName: 'Delhi',
        metro: true,
        tier1: true
    },
    '17': {
        cityName: 'Indore',
        metro: false,
        tier1: false
    }
}

Your '16' and '17' are siblings with configType. You should have it nested inside e.g.

{
    _id: 62e135519567726de42421c2,
    configType: 'cityConfig',
    cityConfig: [
        '16': {
            cityName: 'Delhi',
            metro: true,
            tier1: true
        },
        '17': {
            cityName: 'Indore',
            metro: false,
            tier1: false
        }
    ]
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are using Mongoose, so if you want to return the JavaScript object, you should use lean() method.

const cityConfig = await City.findOne({ configType: "cityConfig" }).lean();

If you don't use lean(), Mongoose will hydrate the document. You can read more here.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can check for keys which are numbers only.

const cityConfig = await City.findOne({
 configType: "cityConfig",
});

console.log("cityConfig - ", cityConfig);

let metroCities = [];

for (var city in cityConfig) {
 if (!isNaN(city)) {
   console.log("city - ", city);
   if (cityConfig[city].metro == true) {
     // throws an error here obviously
     metroCities.push(city);
   }
 }
}
return metroCities;
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda