2016-12-21 4 views
0

私はthisの記事を参考にしましたが、私は何かを犯したと思います。マングースに深く人をつける方法

私は深いモデルカートを移入します:

var CartSchema = new Schema({ 
    products: [{ 
    product: { type: Schema.ObjectId, ref : 'Product' }, 
    quantity: { type: Number, default: 1} 
    }], 
    totalItems: { type: Number, default: 0}, 
    message: { type: String }, 
    client: { type : Schema.ObjectId, ref : 'User' }, 
    time: { type: Date, default: new Date()}, 
    session: { type: String } 
}); 

だから私はproduct.addonsを取得し、

var ProductSchema = new Schema({ 
    name: { type: String, default: '' }, 
    inventory: { type: Number }, 
    type: { type: String, default: 'cesta' }, 
    price: { type: Number, required: true }, 
    discount: { type: Number}, 
    description: [{ 
    item: { type : Schema.ObjectId, ref : 'Item' }, 
    quantity: Number 
    }], 
    photos: { 
    photo1: String, 
    photo2: String 
    }, 
    related_products: [{ type : Schema.ObjectId, ref : 'Product' }], 
    addons: [{ type : Schema.ObjectId, ref : 'Product' }], 
    category: { type: Schema.ObjectId, ref: 'Category' }, 
    code: { type: String }, 
    descricao_avulsa: String, 
    slug: String 
}); 

をproduct.description.item私はこれを試してみたが、行くように見えますいくつかの永遠のループに(それはCONSOLE.LOGしません:

var populate = { 
    path: "products.product", 
    model: 'Product', 
    populate: [ 
     { path: "price name photos slug" }, 
     { path: "description.item addons", model: 'Item'} 
    ] 
}; 
Cart.findOne({session: req.cookies['express:sess']}) 
    .populate(populate) 
    .exec(function(err, cart){ 
    if(err){ 
     return err;  } 
    console.log(cart.products[0].product); 

    next(); 
    }); 

は私もPOのためにこれで、同じコードを試してみましたパルゲート変数:

var populate = [ 
    { path: "products.product", select: "price name photos slug description addons" }, 
    { path: "products.product.description.item", select: "name" }, 
    { path: "products.product.addons", select: "name" } 
]; 

しかし、私が望む結果は得られません。私は私の結果はこのような何かを見てみたい

{ 
    _id: 5859790cc307556218b9d2e1, 
    slug: 'nova-cestinha', 
    price: 14300, 
    addons: [ { name: 'produto' } ], 
    photos: { 
    photo1: 'https://frutacor.s3.amazonaws.com/1482258691162', 
    photo2: 'https://frutacor.s3.amazonaws.com/1482258691189' 
    }, 
    description: 
    [ { 
     item: {name: 'casadinho'}, 
     quantity: 4, 
     _id: 5859790cc307556218b9d2e4 
    }, 
    { 
     item: {name: 'brownie}, 
     quantity: 5, 
     _id: 5859790cc307556218b9d2e3 } 
    ], 
name: 'nova cestinha' 
} 

答えて

1

をあなたは、正しい軌道に乗ってあなたの2番目のコードスニペットでは、単純なミスです。これを試して、目的の結果が得られるかどうかを確認してください:

Cart.findOne({session: req.cookies['express:sess']}) 
.populate({ 
    path : 'products.product', 
    select : 'price name photos slug description addons', 
    populate : [{ path : 'description.item' , select : 'name', model: 'Item'}, 
     { path : 'addons',select : 'name'}] 
}) 
.exec(function(err, cart){ 
... 
}); 
+0

アドオンのために働いています。私はdescription.item、助けをありがとうしようとします。 – iagowp

+0

それは両方のために働いていたはずですが、私は人口の中に複数の人口について知っていませんが、それは問題を引き起こした可能性があります。 –

+1

description.itemに「model: 'Item'」を追加しましたが、それはうまくいかなかったのです。私は本当にstackoverflow上でこれを進めるための最善の方法についてはわからないので、私は正しいので、あなたの答えを編集しようとした、私はそれを受け入れることができます – iagowp

関連する問題