loopbackio / loopbackio/loopback-connector

loopback embedded updating object is not working with $inc

Open
#624 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
35
Forks
102
Avg merge
14h 26m
Merged PRs (30d)
13

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 Item.json and the Branch Wise Available Qty Embedded Schema, then inspect the branchWiseAvailableQtyE.set call using $inc. Reproduce the validation error and compare it with the working literal update. Done means an existing embedded item's availableQty can be incremented or decremented and a missing object can be added without validation errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.