Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.populate() returning empty array?

I’m working on project and I am trying to get the users reviews to display on the page. However whenever I run my application it returns an empty array and I’m not sure why, I have no issue with the getReviews function as it returns everything correctly, but getUserReviews just returns an empty array with no error. I’ve tried multiple methods and just can’t seem to get it

Review Model

    const mongoose = require("mongoose");
    
    const Review = mongoose.model(
      "Review",
      new mongoose.Schema({
        movieId: String,
        reviewId: String,
        content: String,
        sentimentScore: String,
        author: [
          {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
          }
        ],
        reponseTo: [
            {
              type: mongoose.Schema.Types.ObjectId,
              ref: "User"
            },
        ]
      })
    );
    
    module.exports = Review;

User Model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    const mongoose = require("mongoose");
    
    const User = mongoose.model(
      "User",
      new mongoose.Schema({
        username: String,
        email: String,
        password: String,
        roles: [
          {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Role"
          }
        ]
      })
    );
    
    module.exports = User;

Review Routes

    const express =  require('express');
    const router = express.Router();
    const {authJwt} = require("../middlewares");
    const Review = require("../models/review.model")
    
    router.use(function(req, res, next) {
          res.header(
            "Access-Control-Allow-Headers",
            "x-access-token, Origin, Content-Type, Accept"
          );
          next();
    });
    
    
    router.post("/addReview", [authJwt.verifyToken], (req, res) => {
        const review = new Review(req.body)
    
        review.save((err, review) => {
            if(err) return res.json({success:false, err})
            
            Review.find({'_id': review._id})
            .populate('author')
            .exec((err, result) => {
                if(err) return res.json({success: false, err})
                return res.status(200).json({success: true, result})
            })
        })
        
    })
    
    router.post("/getReviews", [authJwt.verifyToken], (req, res) => {
        Review.find({"movieId": req.body.data}) 
        // console.log("ID ", req.body.data)
        .populate('author')
        .exec((err, reviews) => {
            if(err) return res.status(400).send(err)
            res.status(200).json({success: true, reviews})
        })
        
    })

    router.post("/getUserReviews", [authJwt.verifyToken], (req, res) => {
        Review.find({"userId": req.body.data}) 
        .populate({
            path: 'author.user',
            model: 'Review'})
        .exec((err, reviews) => {
            if(err) return res.status(400).send(err)
            res.status(200).json({success: true, reviews})
        })
        
    })

>Solution :

You try to query Review collection with userId field, and Review collection does not have userId field in its model definition.

Maybe you wanted to query author array? In that case, try this:

router.post("/getUserReviews", [authJwt.verifyToken], (req, res) => {
    Review.find({ author: req.body.data }) 
    .populate('author')
    .exec((err, reviews) => {
        if(err) return res.status(400).send(err)
        res.status(200).json({success: true, reviews})
    })
    
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading