'Having some problems with Mongoose Virtuals

I used node.js mongoDB, Express, and mongoose and i have 2 collection with the name of tour and review.
Review collection is child of tour collection. The id of tours save in reviews. child(review) know about the parents(tour) but the parents(tour) does not know anything about children(review) cause i did not stored the ID's on tour.
So i use virtual method to have access reviews on the tour also ,BTW when i use this code i dont have any problem and evrything work just fine:

 tourSchema.virtual('reviews', {
    ref: 'Review',
    foreignField: 'tour',
    localField: '_id',
  });
const tour =  await tours.findOne(---).populate({
    path: 'reviews',
    fields: 'review rating user',
  });
log(tour) // show the tour`
const reviews =  tour.reviews
log(reviews) // show the reviews`

But this code have come with some problems :

tourSchema.pre('findOne', function (next) {
  tourSchema.virtual('reviews', {
    ref: 'Review',
    foreignField: 'tour',
    localField: '_id',
  });
  next();
});
const tour =  await tours.findOne(---).populate({
    path: 'reviews',
    fields: 'review rating user',
  });
log(tour) // show the tour
const reviews =  tour.reviews
log(reviews) // show undefined

Still,I can see the reviews on the tour, When i just log the tour on the console. But when i use tour.reviews it return undefined! look at the results of console.log(tour) closer in the both methods:

console.log(tour) //tour result are same in both method
{
  _id: 5c88fa8cf4afda39709c2955,
  name: 'The Sea Explorer',
  duration: 7,
  maxGroupSize: 15,
  difficulty: 'medium',
  price: 497,
  reviews: [
    {
      _id: 5c8a34ed14eb5c17645c9108,
      review: 'Cras mollis nisi parturient mi nec aliquet suspendisse sagittis eros condimentum scelerisque taciti mattis praesent feugiat eu nascetur a tincidunt',
      rating: 5,
      user: [Object],
      tour: 5c88fa8cf4afda39709c2955,
      createdAt: 2022-05-22T03:25:57.652Z,
      __v: 0,
      id: '5c8a34ed14eb5c17645c9108'
    },
    {
      _id: 5c8a36b714eb5c17645c910f,
      review: 'Pulvinar taciti etiam aenean lacinia natoque interdum fringilla suspendisse nam sapien urna!',
      rating: 4,
      user: [Object],
      tour: 5c88fa8cf4afda39709c2955,
      createdAt: 2022-05-22T03:25:57.658Z,
      __v: 0,
      id: '5c8a36b714eb5c17645c910f'
    },
    {
      _id: 5c8a391f14eb5c17645c911f,
      review: 'Sem feugiat sed lorem vel dignissim platea habitasse dolor suscipit ultricies dapibus',
      rating: 5,
      user: [Object],
      tour: 5c88fa8cf4afda39709c2955,
      createdAt: 2022-05-22T03:25:57.664Z,
      __v: 0,
      id: '5c8a391f14eb5c17645c911f'
    },
    {
      _id: 5c8a3a7014eb5c17645c9124,
      review: 'Blandit varius nascetur est felis praesent lorem himenaeos pretium dapibus tellus bibendum consequat ac duis',
      rating: 5,
      user: [Object],
      tour: 5c88fa8cf4afda39709c2955,
      createdAt: 2022-05-22T03:25:57.667Z,
      __v: 0,
      id: '5c8a3a7014eb5c17645c9124'
    },
    {
      _id: 5c8a3b7c14eb5c17645c912f,
      review: 'Tempor pellentesque eu placerat auctor enim nam suscipit tincidunt natoque ipsum est.',
      rating: 5,
      user: [Object],
      tour: 5c88fa8cf4afda39709c2955,
      createdAt: 2022-05-22T03:25:57.669Z,
      __v: 0,
      id: '5c8a3b7c14eb5c17645c912f'
    },
    {
      _id: 5c8a3cdc14eb5c17645c913b,
      review: 'Magna magnis tellus dui vivamus donec placerat vehicula erat turpis',
      rating: 5,
      user: [Object],
      tour: 5c88fa8cf4afda39709c2955,
      createdAt: 2022-05-22T03:25:57.672Z,
      __v: 0,
      id: '5c8a3cdc14eb5c17645c913b'
    }
  ],
  durationWeeks: 1,
  id: '5c88fa8cf4afda39709c2955'
}
// in the 1st method tour.reviews show the reviews
// but in 2nd method tour.reviews show undefined

BTW , when i use console.log(tour.reviews) it's get undefined. and i can't figure out why it's happening .

    {
   toJSON: { virtuals: true },
   toObject: { virtuals: true },
 }
// this two also turned on at schema


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source