заполнить вложенный массив объектов мангуста - PullRequest
1 голос
/ 06 мая 2019

Я пытался заполнить товары в моей корзине.Моя корзина модели -

const ProductSchema = new mongoose.Schema({
  product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product'},
  quantity: Number
});
const CartSchema = new mongoose.Schema({
  userId: mongoose.Schema.Types.ObjectId,
  products: [ProductSchema]
});

Я пытаюсь получить значение корзины следующим образом -

let getCart = async (userId) => {
  let res = await Cart.find({ userId: userId }).populate('products.product')
  return res;
};

Вывод -

{
  userId: xyz,
  products:[
    product: null,
    quantity:1
  ]
}

Ожидаемый результат -

{ 
  userId: xyz,
  products:[
    product: {
      name: 'product name', 
      description:'',
      ...
    },
    quantity:1
  ]
}

Ответы [ 2 ]

0 голосов
/ 06 мая 2019

Наверное, достаточно просто заполнить товар.

let getCart = async (userId) => {
  let res = await Cart.find({ userId: userId }).populate('products')
  return res;
};
0 голосов
/ 06 мая 2019

Вам нужно заполнить только products и выбрать product для отображения из заполненного массива.

...