Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Virtual populate in JS

Virtual populate in JS

Scheduled Pinned Locked Moved JavaScript
javascriptcssdatabasemongodbxml
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adids1221
    wrote on last edited by
    #1

    Hey, I am trying to implement a virtual populate in my js between two models I created. reviewModel:

    const reviewSchema = new mongoose.Schema({
    review: {
    type: String,
    required: [true, 'Review can not be empty!']
    },
    rating: {
    type: Number,
    min: 1,
    max: 5
    },
    createdAt: {
    type: Date,
    default: Date.now(),
    },
    tour: [
    {
    type: mongoose.Schema.ObjectId,
    ref: 'Tour',
    required: [true, 'Review must be belong to a tour.']
    }
    ],
    user: [
    {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    required: [true, 'Review must be belong to a user.']
    }
    ]
    },
    {
    //passing options, getting the virual properties to the document/object
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
    }
    );

    and my tourModel:

    const tourSchema = new mongoose.Schema({
    name: {
    type: String,
    required: [true, 'A tour must have a name'], //validator
    unique: true,
    trim: true,
    maxlength: [40, 'A tour name must have less or equal then 40 characters'],
    minlength: [10, 'A tour name must have at least 10 character']
    //validate: [validator.isAlpha, 'A tour name must have only alphabetic characters']
    },
    slug: {
    type: String
    },
    duration: {
    type: Number,
    required: [true, 'A tour must have a duration']
    },
    maxGroupSize: {
    type: Number,
    required: [true, 'A tour must have a group size']
    },
    difficulty: {
    type: String,
    required: [true, 'A tour must have a difficuulty level'],
    enum: {
    values: ['easy', 'medium', 'difficult'],
    message: 'Difficulty is either "easy", "medium" or "difficult"'
    }
    },
    ratingsAverage: {
    type: Number,
    default: 4.5,
    min: [1, 'Rating must be above 1'],
    max: [5, 'Rating must be below 5']
    },
    ratingsQuantaity: {
    type: Number,
    default: 0
    },
    price: {
    type: Number,
    required: [true, 'A tour must have a price']
    },
    priceDiscount: {
    type: Number,
    validate: {
    validator: function (val) {
    return val < this.price;
    },
    message: 'Discount price ({VALUE}) shoulld be below the regular price'

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups