Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Empuje los objetos de la matriz al campo del modelo de matriz de mangosta | Mangosta MongoDB

Estoy un poco confundido sobre cómo empujar todos los objetos de la matriz al campo de matriz de mangosta. ¿Tengo que usar bucle?

Creé un modelo de inventario donde la lista de artículos es una matriz de campos de esquema de artículos

 const itemlistSchema: mongoose.Schema = new Schema({ item: { type: Schema.Types.ObjectId, ref: "Item", required: true, }, quantity: { type: Number, required: true, }, }); const inventorySchema: mongoose.Schema = new Schema( { code: { type: String, trim: true, required: true, unique: true, }, user: { type: Schema.Types.ObjectId, ref: "User", required: true, }, shop: { type: Schema.Types.ObjectId, ref: "Shop", required: true, unique: true, }, itemlist: [itemlistSchema], }, { timestamps: true } ); module.exports = retailMongoose.model( "Inventory", inventorySchema, "Inventory" );

Si obtengo una matriz de la interfaz o como entrada, ¿cómo puedo actualizar este inventario con artículos?

Supongamos que obtengo esta matriz desde la interfaz:

 shop_id = "uniqueShopId" items = [ { item:"itemId", quantity:20 }, { item:"another itemID", quantity:11 } ]

¿Cómo puedo enviar esto a mi modelo de inventario? Cada tienda tendrá un solo inventario, por lo que puedo encontrar el inventario por shop_id.

¿Alguien puede ayudar por favor? gracias ❤️

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

He creado esta solución que funciona para mí.

 //add items to inventory.itemslist let itemsList = inventory.itemlist; // console.log("Db itemsList ==>", itemsList); //check if item already exists in inventory for (let i = 0; i < items.length; i++) { const item = items[i]; // find item in inventory var itemInInventory = itemsList.findIndex( (anitemInInventory: any) => // console.log(anitemInInventory.item_id.toHexString(), item.item_id) anitemInInventory.item_id.toHexString() === item.item_id ); // console.log("itemInInventory ==>", itemInInventory); } console.log("Existing Item ==>", itemInInventory); if (itemInInventory > -1) { //send error if item already exists return res.throw_4906_ITEM_ALREADY_EXISTS( "Item already exists inside inventory. Cannot add again ,you can update it" ); } items.forEach((item: any) => { if (!item.item_id || !item.quantity) { return res.throw_5005_INCOMPLETE_REQUIRED_FIELDS( "Adding items invalid config" ); } itemsList.push({ item_id: item.item_id, quantity: parseInt(item.quantity), }); }); inventory.itemlist = itemsList; await inventory.save(); return res.throw_2101_INVENTORY_UPDATE_SUCCESS("Items added to inventory", { inventory, });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda