1

I am trying to add data into a array within a document everytime the user makes a post request. However, whenever the user makes the post request, the data in question is not added to the embedded array as intended. And I keep getting this error message in postman whenever it fails to do so:

{
    "message": "Converting circular structure to JSON",
    "error": {}
}

For reference, here is the user model schema file:

const uuidV4 = require('uuid/v4');
const {reviewSchema} = require('../models/review');
const {recipeSchema} = require('../models/recipe');
let Schema = mongoose.Schema;



let User = new Schema({

    name: {
        type: String
    },

    username: {
        type: String,
        unique: true,
        required: true
    },

    password: {
        type: String,
        required: true,
        minlength: 6
    },

    userId: {
        type: String,
        default: "Empty"
    },

    tokens: [{

        access: {
            type: String,
            required: true
        },

        token: {
            type: String,
            required: true
        }

    }],

    profilePic: {
        type: String,
    },

    email: {
        type: String,
        unique: true,
        required: true,
        validate: {
            validator: (value) => {
                return validator.isEmail(value);
            },

            message: `{value} is not a valid email address.`
        }
    },

    admin: {
        type: Boolean,
        defualt: false
    },


    usersFavouriteRecipes: {
        type: Array
    },




    chefKarma: {
        type: Number,
        default: 0
    }


});


let options = ({missingPasswordError: "Incorrect password, try again"});

User.plugin(passportLocalMongoose, options);

module.exports = mongoose.model('User', User);

Here is my code, where I am trying to add data to the embedded array, but instead keep getting the "Converting circular structure to JSON" error :

        router.put('/:userid/:recipeId/', (req, res) => {



            let overallRating = (Number(req.body.howGoodTaste) + Number(req.body.wouldMakeAgain) + Number(req.body.howEasyToMake))  / 3;

            // the User first makes his review of the recipe

            Review.update({postedBy: req.params.userid, reviewOf: req.params.recipeId}, {$set: {
                howEasyToMake: req.body.howEasyToMake,
                wouldMakeAgain: req.body.wouldMakeAgain,
                howGoodTaste: req.body.howGoodTaste,
                rating: overallRating,
                postedBy: req.params.userid,
                reviewOf: req.params.recipeId,
                comment: req.body.comment
            }},{upsert: true, setDefaultsOnInsert: true}, (err, doc) => {

                if (err) res.status(400).send(err);

            });

        /*
            Then, if his or her review of the recipe is 4 or 5 stars
            , the person who submitted the recipe has a point added their chefKarma, 
            and the _id of the recipe is added to the usersFavouriteRecipes array in 
            the user document. 


    */ 

            if (overallRating > 3) {

                        Recipe.find({_id: req.params.recipeId}, {postedBy: 1}, (err, poster) => {
            if (err) res.status(400).send(err);

            console.log(poster.postedBy);

            User.update({_id: poster.postedBy}, {
                $inc: {
                    chefKarma: 1
                }});

        });

/*

Whenever I console.log(poster.postedBy) it returns undefined.
Whenever I console.log(poster) it returns { _id: 594184662a932a1d287aac8a,
  postedBy: '594181762b5b6d043c211a88' }.
*/


                User.update({_id: req.params.userid}, {$push: {usersFavouriteRecipes: req.params.recipeId}});



            }

        });

Can anyone help me? It would be greatly appreciated

MountainSlayer
  • 291
  • 1
  • 5
  • 14
  • Somewhere you're doing a circular reference, and your json-methods can't convert that. See this answer: https://stackoverflow.com/a/4816258/7296909 – Jesper Jun 14 '17 at 21:34
  • How can I get around this problem? – MountainSlayer Jun 14 '17 at 21:46
  • Hard to tell since I don't know how the post-body looks like. Have you tried logging it before updating it in the db? – Jesper Jun 14 '17 at 21:58
  • I made some changes, I'll edit this question to reflect that. But so far I keep getting undefined whenever I try to console.log the recipe.postedBy – MountainSlayer Jun 14 '17 at 22:09
  • 1
    Whenever I console.log(poster.postedBy) it returns undefined. Whenever I console.log(poster) it returns { _id: 594184662a932a1d287aac8a, postedBy: '594181762b5b6d043c211a88' } – MountainSlayer Jun 14 '17 at 22:16
  • I need someway to just get the postedBy value and I was hoping field projection could help me with that. – MountainSlayer Jun 14 '17 at 22:18
  • Why you're getting undefined is probably cause the poster-object is in json right? Otherwise, an object and it's key value shouldn't return undefined. – Jesper Jun 14 '17 at 22:39

0 Answers0