loopbackio / loopbackio/loopback-datasource-juggler

$inc is not working in embedded object loopback 3

Open
#2,319 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
282
Forks
366
Avg merge
17h 4m
Merged PRs (30d)
10

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Item.json embedsMany relation and the BranchWiseAvailableQtyE embedded schema, then reproduce the validation error at the branchWiseAvailableQtyE.set call using the shown $inc value. Trace how embedded-object updates are validated and determine whether incrementing availableQty and adding a missing object are supported; done means the supported behavior or required change is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.