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