0

I am little confused how to push all objects from array to mongoose array field. Do I have to use loop?

I have created an inventory model where itemlist is an array of itemschema field

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"
);

If I get an array from frontend or as input how can I update this inventory with items.

Suppose I get this array from frontend:

shop_id = "uniqueShopId"

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

How can I push this to my inventory model. Every shop will have single inventory , so I can find inventory by shop_id.

Can any one help please? Thank you ❤️

5
  • Code will be generated by nanoid() Commented May 25, 2022 at 9:48
  • 1
    you can directly assign the items array to yourmodel.itemList field and after that call the yourmodel.save() method. Let me know If this solution wouldn't work. This answer also can help you Commented May 25, 2022 at 9:59
  • Yea Its working , but i have added unique to item field at itemlistSchema, Why its still taking duplicate entries Commented May 25, 2022 at 10:38
  • But you didn't add unique to itemList Commented May 25, 2022 at 10:44
  • I added later its not in the qstn. Commented May 25, 2022 at 10:49

1 Answer 1

0

I have created this solution which works for me.

 //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,
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.