const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const { ObjectId } = mongoose.Schema.Types;
const reviewSchema = new Schema(
{
productId: {
type: ObjectId,
ref: "Product",
},
reviews: [
{
userId: {
type: ObjectId,
ref: "User",
},
rating: {
type: Number,
required: true,
},
comment: {
type: String,
},
},
],
totalRate: {
type: Number,
default: 0,
},
},
{ timestamps: true }
);
router.get("/:id", (req, res) => {
Review.findOne({ productId: req.params.id })
.populate({
path: "reviews",
populate: { path: "userId", select: "-_id username" },
})
.then((reviews) => {
res.status(200).json(reviews);
})
.catch((err) => res.status(500).json(err));
});
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter