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

126
Views
¿Cómo llenar la mangosta con obejectId que se define como Número?

Estoy tratando de completar mis solicitudes de usuario.profileId pero solo devuelve valores nulos.

Tengo los siguientes esquemas:

Primer esquema:

 const profileSchema = new mongoose.Schema({ _id: { type: Number }, //<- _id is defined as a number which represents mobile number (easier for me to handle) first: { type: String }, second: { type: String }, }); module.exports = mongoose.model('Profile', profileSchema, 'profiles');

Segundo esquema:

 const userSchema = new mongoose.Schema({ firstName: { type: String }, lastName: { type: String }, requests: [ { profileId: { type: mongoose.Schema.Types.ObjectId, ref: 'Profile', }, requestTime: { type: Date, default: Date.now }, }, ], }); module.exports = mongoose.model('User', userSchema, 'users');

Aquí está mi código:

 const user = await User.findById(req.user).populate('requests.profileId'); console.log(user.requests);

Aquí está la salida:

 [ { _id: 6201633869648e2b74c00a10, profileId: null, requestTime: 2022-02-07T18:21:44.722Z }, { _id: 6201633b69648e2b74c00a11, profileId: null, requestTime: 2022-02-07T18:21:47.238Z }, { _id: 620238f9d2b5dd3dee6c41a2, profileId: null, requestTime: 2022-02-08T09:33:45.176Z }, { _id: 620239253220343dfd7cfdd9, profileId: null, requestTime: 2022-02-08T09:34:29.780Z } ]

Aquí está la salida sin poblar:

 [ { _id: 6201633869648e2b74c00a10, profileId: 393732353235303134343330, //<- typeof profileId is obeject requestTime: 2022-02-07T18:21:44.722Z }, { _id: 6201633b69648e2b74c00a11, profileId: 393732353435353333313131, requestTime: 2022-02-07T18:21:47.238Z }, { _id: 620238f9d2b5dd3dee6c41a2, profileId: 393732353435353333313131, requestTime: 2022-02-08T09:33:45.176Z }, { _id: 620239253220343dfd7cfdd9, profileId: 393732353435353333313131, requestTime: 2022-02-08T09:34:29.780Z } ]

Actualmente Profile.findById(mobileNumber) funciona bien.

¿Alguna idea de lo que salió mal?

Apreciaré mucho su ayuda.

Gracias por adelantado :)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Prueba esto podría funcionar, avísame si no funciona

 const user = await User.findById(req.user).populate('Profile'); console.log(user.requests);
about 4 years ago · Juan Pablo Isaza Report

0

prueba esto:

 User.findById(req.user).populate({ path: 'requests', populate: { path: 'profileId', model: 'Profile' } })
about 4 years ago · Juan Pablo Isaza Report

0

¡Para futuros lectores que tengan el mismo problema!

He encontrado una solución para este problema.

Tuve que cambiar el tipo de ID de perfil a Número en mi esquema de usuario:

 const userSchema = new mongoose.Schema({ firstName: { type: String }, lastName: { type: String }, requests: [ { profileId: { type: Number // and NOT use type: mongoose.Schema.Types.ObjectId, ref: 'Profile', }, requestTime: { type: Date, default: Date.now }, }, ], }); module.exports = mongoose.model('User', userSchema, 'users');

¡Ahora funciona!

about 4 years ago · Juan Pablo Isaza 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!