Mongoose模式模式引用不工作

基本上我有3个mongoose模型(用户,产品,购物车)。我已经尝试用用户模型引用购物车模型。我用mongoose中的填充方法来填充模型中的引用属性。但它不工作,在安装后将产品添加到购物车之后获得一个空的购物车arrays。

{ "_id": "5a0b2dcf726753258002273d", "password": "12345", "securityQuestion": "What is your Nick Name?", "securityAnswer": "rj1", "__v": 0, "cart": [], // empty cart here "mobileNumber": 1234567890, "email": "rahul@gmail.com", "lastName": "jhawar", "firstName": "rahul", "userName": "rj" } 

我也想知道我的模型引用是否正确(我的意思是关系)。

我对这个概念很陌生,请帮助我。

  //Cart.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var cartSchema = new Schema({ productName : {type:String,default:''}, productCategory : {type:String,default:''}, productDescription : {type:String,default:''}, productPrice : {type:String,default:''}, sellerName : {type:String,default:''}, }); module.exports=mongoose.model('Cart',cartSchema); //User.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var userSchema = new Schema({ userName : {type:String,default:'',required:true}, firstName : {type:String,default:''}, lastName : {type:String,default:''}, email : {type:String,default:''}, mobileNumber : {type:Number,default:''}, password : {type:String}, securityQuestion : {type:String}, securityAnswer : {type:String}, cart : [{ type: Schema.Types.ObjectId, ref:'Cart' }] }); module.exports=mongoose.model('User',userSchema); ///Routes/// const cartRouter = express.Router(); const cartModel = mongoose.model('Cart'); const userModel = mongoose.model('User'); const productModel = mongoose.model('Product'); cartRouter.get('/addtocart/:id',auth.checkLogin,(req,res)=>{ productModel.findOne({'_id':req.params.id},(err,foundProduct)=>{ if(!err){ const newCartProduct = new cartModel({ productName : foundProduct.productName, productCategory : foundProduct.productCategory, productDescription : foundProduct.productDescription, productPrice : foundProduct.productPrice, sellerName : foundProduct.sellerName }); newCartProduct.save((err)=>{ if(!err){ userModel.findOne({'_id':req.session.user._id}).populate('cart') . exec((err,cartproduct)=>{ console.log(JSON.stringify(cartproduct, null, "\t")); }); console.log('New Product added to cart'); res.redirect('/users/cart_products'); } }); }); }); 

您已经正确设置了用户与购物车模型之间的关系,因为用户可能持有对多个购物车模型的引用。

但是,保存购物车时,实际上并未更新用户的购物车arrays/属性。 您必须同时保存您的newCartProduct,并且还必须将生成的id推入购物车arrays,然后保存您的用户模型。 如果你不这样做,填充方法不会工作,因为它没有什么可填充的。

下面是一个如何使用findByIdandUpdate来更新新购物车项目的用户的例子。

 cartRouter.get('/addtocart/:id',auth.checkLogin,(req,res)=>{ productModel.findOne({'_id':req.params.id},(err,foundProduct)=>{ if(!err){ const newCartProduct = new cartModel({ productName : foundProduct.productName, productCategory : foundProduct.productCategory, productDescription : foundProduct.productDescription, productPrice : foundProduct.productPrice, sellerName : foundProduct.sellerName }); newCartProduct.save((err, o)=>{ if(!err){ userModel.findByIdAndUpdate(req.session.user._id, {$push: {"cart": o._id}}, {safe: true, new : true}) .populate('cart') .exec((err, model) => { console.log(model); //This will have the cart array filled in }); }); }); 

其他select可以是反转关系,将参考挂在购物车上,也可以使用embedded的子文档:

 cart: [Cart] 

如果您从不需要在单个用户的上下文之外访问购物车条目(如果您不需要跨用户汇总信息),那么这可能是正确的做法。

您的方法可能适合您的使用案例,但是当从购物车中添加和删除项目时,必须手动维护双方的关系。