2 回答

TA貢獻1869條經驗 獲得超4個贊
您的var pathNodesArray = [];范圍不對。您希望每條路徑都有自己的路徑節點數組,雖然pathNodesArray = [];在循環中執行順序操作可能有效,但在并行處理路徑時會失敗。const更喜歡var:
const PathNode = require('../models/pathNode');
const Path = require('../models/path');
const repository = require('../../repository');
const jsonConverted = JSON.parse(convert.xml2json(req.file.buffer.toString(), { compact: true, spaces: 4, alwaysChildren: true }));
const paths = jsonConverted.GlDocumentInfo.world.GlDocument.GlDocumentNetwork.Network.Paths.Path;
await Promise.all(paths.map(async path => {
? ? const pathNodesArray = [];
//? ^^^^^ declare inside each iteration
? ? await Promise.all(path.PathNodes.PathNode.map(async pathNode => {
? ? ? ? const newPathNode = new PathNode({
? ? ? ? ? ? key: pathNode._attributes.key,
? ? ? ? ? ? node: pathNode._attributes.Node,
? ? ? ? ? ? duration: pathNode._attributes.Duration,
? ? ? ? ? ? distance: pathNode._attributes.Distance,
? ? ? ? });
? ? ? ? pathNodesArray.push(newPathNode.key);
? ? ? ? await repository.savePathNode(newPathNode);
//? ? ? ^^^^^
? ? }));
? ? const newPath = new Path({
? ? ? ? key: path._attributes.key,
? ? ? ? isEmpty: path._attributes.IsEmpty.toString().toLowerCase(),
? ? ? ? pathNodes: pathNodesArray,
? ? });
? ? await repository.savePath(newPath);
//? ^^^^^
}));
// … doesn't matter for this question

TA貢獻1909條經驗 獲得超7個贊
嘗試添加return之前repository.savePathNode(newPathNode)?,F在,它接縫了,你的嵌套承諾沒有被等待
...
await Promise.all(path.PathNodes.PathNode.map(async pathNode => {
var newPathNode = new PathNode();
newPathNode.key = pathNode._attributes.key;
newPathNode.node = pathNode._attributes.Node;
newPathNode.duration = pathNode._attributes.Duration;
newPathNode.distance = pathNode._attributes.Distance;
pathNodesArray.push(newPathNode.key);
return repository.savePathNode(newPathNode);
}))
...
添加回答
舉報