Skip to content

loopback embedded updating object is not working with $inc #624

Open
@MrShadow50

Description

@MrShadow50

I am using loopback 3. In which I have model Item and that have embedded object availableQtyBranchWise. I was trying to update the availableQtyBranchWise using $inc but with this operator i got validation error.

Item.json

{
  "name": "Item",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "strictObjectIDCoercion": true
  },
  "properties": {
    "name": {
      "type": "string"
    },
    "barcode": {
      "type": "number"
    }
  },
  "relations": {
    "branchWiseAvailableQtyE": {
      "type": "embedsMany",
      "model": "BranchWiseAvailableQtyE",
      "property": "branchWiseAvailableQty",
      "options": {
        "validate": false,
        "forceId": true
      },
      "description": "branchWiseAvailableQtyE is used to store branch wise available quantity"
    }
  }
}

Branch Wise Available Qty Embedded Schema

{
  "name": "BranchWiseAvailableQtyE",
  "base": "Model",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "refBranchName": {
      "type": "string",
      "required": true
    },
    "refBranchId": {
      "type": "string",
      "required": true
    },
    "refBranchCode": {
      "type": "string",
      "required": true
    },
    "availableQty": {
      "type": "number",
      "required": true,
      "default": 0
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

In DB Data should look like

{
  "_id": {
    "$oid": "66e90d94c2d97907244dfae0"
  },
  "name": "01 PlK 1 1000 SERIES",
  "branchWiseAvailableQty": [
    {
      "refBranchName": "Delhi",
      "refBranchId": {
        "$oid": "6204c8d8e1300e2ea8420483"
      },
      "refBranchCode": "DL",
      "availableQty": 20,
      "id": {
        "$oid": "f3ef503f555877c69cc8ed6a"
      }
    },
 {
      "refBranchName": "Mumbai",
      "refBranchId": {
        "$oid": "6204c8d8e1340e2ea8420483"
      },
      "refBranchCode": "MU",
      "availableQty": 40,
      "id": {
        "$oid": "f3ef503f555987c69cc8ed6a"
      }
    }
  ],
  "barcode": 1856
}

I was trying to increment, decrement availableQty when object exists and add object when does not exist.

This is my code where I update the availableQty

    item.branchWiseAvailableQtyE.set(ObjectId(f3ef503f555987c69cc8ed6a),{ availableQty: {$inc: 10}}, null, function(err, result) {
      if (err) {
        return reject(err);
      }
      return resolve(result);
    });

This give me validation error because I am using

{ availableQty: {$inc: 10}}
image

But If I use this syntax it works

{ availableQty: 10}

Please help me. Thanks, in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions