- from ERP system ---- "Altria-Serv"
positions
.map((p) => p.parent)
.forEach((org) =>
data.filter((d) => d._id.toString() === org.toString()).forEach((i) => (i.isLeaf = false))
);
- from ERP system ---- "Altria-Serv"
MenuSchema.static('populateTree', async function () {
const nodes = await this.find({
$or: [{ parent: null }, { parent: { $exists: false } }]
});
for (const node of nodes) {
node.children = await node.populateTree();
}
return nodes;
});
MenuSchema.methods.populateTree = function () {
const node = this;
if (!node) return [];
return this.constructor
.find({ parent: this._id })
.exec()
.then(function (arrayOfChildren) {
return Promise.each(arrayOfChildren, function (child) {
if (Array.isArray(node.children)) {
node.children.push(child);
} else {
node.children = [child];
}
child.parent = null;
return child.populateTree();
});
});
};
/**
* 处理树形节点为指定格式
* @param {Array<Object>} nodes 树形节点数组
* @param {Function} func 转换函数
*/
const handleTreeNodes = (nodes, func) =>
nodes.reduce((acc, cur) => {
if (Array.isArray(cur.children) && cur.children.length > 0) {
cur.children = handleTreeNodes(cur.children);
}
if (func && typeof func === 'function') {
acc.push(func(cur));
} else {
acc.push(transformFunc(cur));
}
return acc;
}, []);
mongoose & async iterator
const { RepairIssue } = this.ctx.model;
const allIssues = RepairIssue
.find({})
.populate({ path: 'elevator', select: 'controlUnit adminOrgUnit' });
for await (const issues of allIssues) {
const { elevator, adminOrgUnit, controlUnit } = issues;
if (!adminOrgUnit.equals(elevator.adminOrgUnit)) {
issues.adminOrgUnit = elevator.adminOrgUnit;
issues.save();
}
}