Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongoose virtual population not working correctly. #484

Open
majidali129 opened this issue May 7, 2024 · 0 comments
Open

mongoose virtual population not working correctly. #484

majidali129 opened this issue May 7, 2024 · 0 comments

Comments

@majidali129
Copy link

I've check all related questions on stackoverflow and also visit the mongoose docs, everything seems to be good. But the problem is when i'm trying to get tour by id.

if i don't add populate to end of query inside the getTourById, then it should give me reviews as null. but it did'nt give me anything.
when i added pupulate to end of query, it it gave me an array of all reviews. here i got confused why it gave me all reviews? as i have 7 tours in DB. why it repeat the same review 7 times and only one review gave me required result.

tourSchema code:

i```
mport mongoose, { mongo } from 'mongoose';
import slugify from 'slugify';
import { User } from './user.model.js';

const tourSchema = mongoose.Schema(
{
name: {
type: String,
required: true,
unique: true,
trim: true,
maxLength: [40, 'A tour name must have equal or less than 40 characters'],
minLength: [10, 'A tour name must have equal or more than 10 characters']
},
slug: String,
duration: {
type: Number,
required: [true, 'A tour must have a duration']
},
secretTour: {
type: Boolean,
default: false
},
maxGroupSize: {
type: Number,
required: [true, 'A tour must have a group size']
},
price: {
type: Number,
required: [true, 'A tour must have a price']
},
difficulty: {
type: String,
required: [true, 'A tour must have difficulty level'],
enum: {
values: ['easy', 'medium', 'difficult'],
message: 'Difficulty can be either easy, medium or difficult'
}
},
ratingsAverage: {
type: Number,
default: 4.2,
min: [1, 'Rating must be above 1.0'],
max: [5, 'Rating must be below 5.0']
},
ratingsQuantity: {
type: Number,
default: 0
},
priceDiscount: {
type: Number,
validate: function (val) {
return val < this.price;
},
message: 'Discount price {VALUE} should be less than regular price'
},
summary: {
type: String,
trim: true,
required: [true, 'A tour must have a summary']
},
description: {
type: String,
trim: true
},
imageCover: {
type: String,
required: [true, 'A tour must have a cover image']
},
images: [String],
startDates: [Date],
startLocation: {
type: {
type: String,
default: 'Point',
enum: ['Point']
},
coordinates: [Number],
address: String,
description: String
},
locations: [
{
type: {
type: String,
default: 'Point',
enum: ['Point']
},
coordinates: [Number],
address: String,
description: String,
day: Number
}
],
guides: [
{
type: mongoose.Schema.ObjectId,
ref: 'User'
}
]
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);

tourSchema.virtual('durationInWeeks').get(function () {
return this.duration / 7;
});

// Virtually population
tourSchema.virtual('reviews', {
ref: 'Review',
localField: '_id',
foreignField: 'tour'
});
export const Tour = mongoose.model('Tour', tourSchema);

tourController code:

const getTour = asyncHandler(async (req, res, next) => {
const tour = await Tour.findById(req.params.id).populate('reviews');
if (!tour) {
return next(new appError('Tour not found for that ID', 404));
}
res.status(200).json({
message: 'success',
requestedAt: req.requestTime,
data: {
tour
}
});
});

reviewModel code:

import mongoose from 'mongoose';

const reviewSchema = mongoose.Schema(
  {
    review: {
      type: String,
      required: [true, "Can't post an empty review"]
    },
    rating: {
      type: Number,
      min: 1,
      max: 5
    },
    tour: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Tour',
      required: [true, 'Review must belong to a tour']
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: [true, 'Review must belong to a user']
    }
  },
  { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
);

reviewSchema.pre(/^find/, function (next) {
  this.populate({
    path: 'tour',
    options: { select: 'name' }
  }).populate({
    path: 'user',
    options: { select: 'name photo' }
  });
  next();
});

export const Review = mongoose.model('Review', reviewSchema);
reviewController code: 

import { Review } from '../models/review.model.js';
import { appError } from '../utils/appError.js';
import { asyncHandler } from '../utils/asyncHandler.js';

const getAllReviews = asyncHandler(async (req, res, next) => {
const reviews = await Review.find();
res.status(200).json({
status: 'success',
results: reviews.length,
data: {
reviews
}
});
});

const addNewReview = asyncHandler(async (req, res, next) => {
const review = await Review.create(req.body);
if (!review) return next(new appError('Failed to add review. Please try again', 403));
res.status(201).json({
status: 'success',
data: {
review
}
});
});

export { getAllReviews, addNewReview };


Output after sending request for a tour by id:

{
"message": "success",
"requestedAt": "2024-05-07T11:42:37.442Z",
"data": {
"tour": {
"startLocation": {
"type": "Point",
"coordinates": [
-106.822318,
39.190872
],
"address": "419 S Mill St, Aspen, CO 81611, USA",
"description": "Aspen, USA"
},
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"duration": 4,
"secretTour": false,
"maxGroupSize": 10,
"price": 997,
"difficulty": "difficult",
"ratingsAverage": 4.5,
"ratingsQuantity": 6,
"summary": "Exciting adventure in the snow with snowboarding and skiing",
"description": "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum!\nDolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, exercitation ullamco laboris nisi ut aliquip. Lorem ipsum dolor sit amet, consectetur adipisicing elit!",
"imageCover": "tour-3-cover.jpg",
"images": [
"tour-3-1.jpg",
"tour-3-2.jpg",
"tour-3-3.jpg"
],
"startDates": [
"2022-01-05T10:00:00.000Z",
"2022-02-12T10:00:00.000Z",
"2023-01-06T10:00:00.000Z"
],
"locations": [
{
"type": "Point",
"coordinates": [
-106.855385,
39.182677
],
"description": "Aspen Highlands",
"day": 1,
"_id": "5c88fa8cf4afda39709c295c",
"id": "5c88fa8cf4afda39709c295c"
},
{
"type": "Point",
"coordinates": [
-106.516623,
39.60499
],
"description": "Beaver Creek",
"day": 2,
"_id": "5c88fa8cf4afda39709c295b",
"id": "5c88fa8cf4afda39709c295b"
}
],
"guides": [],
"createdAt": "2024-05-07T05:58:58.291Z",
"updatedAt": "2024-05-07T05:58:58.291Z",
"slug": "The-Snow-Adventurer",
"__v": 0,
"durationInWeeks": 0.5714285714285714,
"reviews": [
{
"_id": "5c8a37b114eb5c17645c9111",
"review": "Ex a bibendum quis volutpat consequat euismod vulputate parturient laoreet diam sagittis amet at blandit.",
"rating": 4,
"tour": {
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"guides": [],
"durationInWeeks": null,
"id": "5c88fa8cf4afda39709c295a"
},
"user": null,
"createdAt": "2024-05-07T06:01:10.946Z",
"updatedAt": "2024-05-07T06:01:10.946Z",
"__v": 0,
"id": "5c8a37b114eb5c17645c9111"
},
{
"_id": "5c8a382d14eb5c17645c9116",
"review": "Semper blandit felis nostra facilisi sodales pulvinar habitasse diam sapien lobortis urna nunc ipsum orci.",
"rating": 5,
"tour": {
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"guides": [],
"durationInWeeks": null,
"id": "5c88fa8cf4afda39709c295a"
},
"user": null,
"createdAt": "2024-05-07T06:01:10.946Z",
"updatedAt": "2024-05-07T06:01:10.946Z",
"__v": 0,
"id": "5c8a382d14eb5c17645c9116"
},
{
"_id": "5c8a355b14eb5c17645c9109",
"review": "Tempus curabitur faucibus auctor bibendum duis gravida tincidunt litora himenaeos facilisis vivamus vehicula potenti semper fusce suspendisse sagittis!",
"rating": 4,
"tour": {
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"guides": [],
"durationInWeeks": null,
"id": "5c88fa8cf4afda39709c295a"
},
"user": null,
"createdAt": "2024-05-07T06:01:10.946Z",
"updatedAt": "2024-05-07T06:01:10.946Z",
"__v": 0,
"id": "5c8a355b14eb5c17645c9109"
},
{
"_id": "5c8a3a9914eb5c17645c9126",
"review": "Netus eleifend adipiscing ligula placerat fusce orci sollicitudin vivamus conubia.",
"rating": 5,
"tour": {
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"guides": [],
"durationInWeeks": null,
"id": "5c88fa8cf4afda39709c295a"
},
"user": null,
"createdAt": "2024-05-07T06:01:10.947Z",
"updatedAt": "2024-05-07T06:01:10.947Z",
"__v": 0,
"id": "5c8a3a9914eb5c17645c9126"
},
{
"_id": "5c8a3b9f14eb5c17645c9130",
"review": "Tempor pellentesque eu placerat auctor enim nam suscipit tincidunt natoque ipsum est.",
"rating": 5,
"tour": {
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"guides": [],
"durationInWeeks": null,
"id": "5c88fa8cf4afda39709c295a"
},
"user": null,
"createdAt": "2024-05-07T06:01:10.948Z",
"updatedAt": "2024-05-07T06:01:10.948Z",
"__v": 0,
"id": "5c8a3b9f14eb5c17645c9130"
},
{
"_id": "5c8a3c3b14eb5c17645c9135",
"review": "Massa orci lacus suspendisse maximus ad integer donec arcu parturient facilisis accumsan consectetur non",
"rating": 4,
"tour": {
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"guides": [],
"durationInWeeks": null,
"id": "5c88fa8cf4afda39709c295a"
},
"user": null,
"createdAt": "2024-05-07T06:01:10.948Z",
"updatedAt": "2024-05-07T06:01:10.948Z",
"__v": 0,
"id": "5c8a3c3b14eb5c17645c9135"
},
{
"_id": "663a11fba58abfda2626679b",
"review": "This review sample ",
"rating": 5,
"tour": {
"_id": "5c88fa8cf4afda39709c295a",
"name": "The Snow Adventurer",
"guides": [],
"durationInWeeks": null,
"id": "5c88fa8cf4afda39709c295a"
},
"user": {
"_id": "6639cbfcbec906df9298a897",
"name": "Guest User"
},
"createdAt": "2024-05-07T11:35:23.399Z",
"updatedAt": "2024-05-07T11:35:23.399Z",
"__v": 0,
"id": "663a11fba58abfda2626679b"
}
],
"id": "5c88fa8cf4afda39709c295a"
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant