Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$inc is not working in embedded object loopback 3 #2319

Open
MrShadow50 opened this issue Oct 16, 2024 · 0 comments
Open

$inc is not working in embedded object loopback 3 #2319

MrShadow50 opened this issue Oct 16, 2024 · 0 comments
Labels

Comments

@MrShadow50
Copy link

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

@MrShadow50 MrShadow50 added the bug label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant