I need to retrieve an array of objects with a specific ID from my database within a server route, and then update a property of an object within that array (rather than returning just the objectID, I want to return the Document as an object with the specified ID).
Below is the code I've written for this task:
let orders = await Order.find({restaurant: restaurantID, status: 'PROCESSING'})
for(let order of orders){ //iterate through all orders
for(let element of order.dishes){ //iterate through order.dishes array (called 'element' since the array contains objects)
let dish = await Dish.findOne({_id: element._id})
element['dish'] = dish //create new property for the dish object
delete element._id //remove the ID property since it already exists inside the element.dish object
}
}
Each order
object within orders
includes an array named dishes
, which holds objects with the properties amount
and an id
. To make it more manageable for the frontend, I aim to remove the ID property, replacing it with a new property called 'dish', holding the referenced dish object.
The challenge lies in manipulating the content of the orders
array. While inspecting orders
by converting it to JSON and sending it in the response, I obtain a JSON array of order objects. However, when I implement the provided code snippet, it fails to alter anything within my orders.
During debugging, the elements within my for loops appear as follows:
EmbeddedDocument {__parentArray: Proxy, __index: 0, $__parent: model, $__: InternalCache, $isNew: false, …}
. Upon conversion to JSON, the desired output is achieved: {"amount":1,"_id":"6183b84fec1c3e109a2271be"}
.
Is orders
truly an array in this scenario? If not, what would be the most efficient way to manipulate it or obtain the documents as an array?
The following represents how orders
appears while being monitored in the debug window:
price (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
price (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
restaurant (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
restaurant (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
status (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
status (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
timestamp (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
timestamp (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
user (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
user (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
__v (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
__v (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
__proto__:Model
length:1
In Postman, the response body looks similar to the desired format but only contains the id instead of the full dishes object:
[
{
"_id": "6183b84fec1c3e109a2271bd",
"user": "6166bc426181646198fc483c",
"restaurant": "6176947ce8b10986b018930e",
"dishes": [
{
"amount": 1,
"_id": "6183b84fec1c3e109a2271be"
},
{
"amount": 2,
"_id": "6183b84fec1c3e109a2271bf"
}
],
"price": 30,
"status": "PROCESSING",
"timestamp": "2021-11-04T10:39:11.800Z",
"__v": 0
}
]
If this issue has been addressed elsewhere, kindly share the link to the relevant question/answer. I have devoted several hours attempting to resolve this matter and have explored various questions without finding a suitable solution.