I have the following code in node.js endpoint
let nodes = {};
if (req.query.fetchType == "tree") {
nodes = await req.groupDocParam.getChildrenTree({ options: { lean: true } });
if(req.query.includeOrganization == "true"){
let data = await Group.findOne({ groupId: req.groupDocParam.groupId })
data.children = [...nodes]
nodes.splice(0, nodes.length)
nodes.push(data)
}
} else if (req.query.fetchType == "children") {
nodes = await req.groupDocParam.getImmediateChildren({});
}
else {
nodes = await req.groupDocParam;
}
res.status(200).json({
message: 'Fetched groups successfully.',
items: nodes
});
At these four lines
let data = await Group.findOne({ groupId: req.groupDocParam.groupId })
data.children = [...nodes]
nodes.splice(0, nodes.length)
nodes.push(data)
I expect nodes array to contain an object with new property children
in it, and it does. However, when I test the endpoint, the newly added children
property is not returned with payload:
Expected
items = [
{
"id": 21,
"name": "test"
// this is from line data.children = [...nodes]
"children": [Object, Object]
}
]
Actual
items = [
{
"id": 21,
"name": "test"
}
]
Is there some immutability issue here in nodes object?