每次用户发出帖子请求时,我都会尝试将数据添加到文档中的数组中。但是,每当用户发出post请求时,所讨论的数据都不会按预期添加到嵌入式阵列中。每当它不能这样做时,我就会在邮递员中收到此错误消息:
{
"message": "Converting circular structure to JSON",
"error": {}
}
供参考,这是用户模型模式文件:
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);
这是我的代码,我试图将数据添加到嵌入式数组,而是继续获得"将循环结构转换为JSON"错误:
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}});
}
});
任何人都可以帮助我吗?非常感谢