Home > Back-end >  How to populate mongoose with obejectId which is defined as Number?
How to populate mongoose with obejectId which is defined as Number?

Time:02-09

I'm trying to populate my user requests.profileId but it returns only nulls.

I have the following schemas:

First Schema:

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');

Second Schema:

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');

Here is my code:

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

Here is the output:

[
  {
    _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
  }
]

Here is the output without populate:

[
  {
    _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
  }
]

Currently Profile.findById(mobileNumber) works fine.

Any ideas what went wrong?

Will greatly appreciate your assistance.

Thanks in advance :)

CodePudding user response:

Try this might work let me know if it doesn't

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

CodePudding user response:

try this:

User.findById(req.user).populate({
    path: 'requests',           
    populate: {
        path:  'profileId',
        model: 'Profile' 
    }
  })

CodePudding user response:

For future readers having the same issue!

I've found a solution for this issue.

I had to change the profileId type to Number in my userSchema:

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');

Now it works!

  •  Tags:  
  • Related