亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我需要 Promise 中的 Promise 及其 .then 先運行

我需要 Promise 中的 Promise 及其 .then 先運行

侃侃爾雅 2023-06-15 16:49:12
所以,我正在閱讀這個 XML 文件:<GlDocumentInfo xmlns:opt="Opt.GL.Domain" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="Opt.GL.BusinessLogic">  <world version="1.0.5">    <GlDocument key="GlDocument:1">        <GlDocumentNetwork>            <Network key="Network:1">                <Paths>                    <Path key="Path:1" IsEmpty="False">                        <PathNodes>                            <PathNode key="PathNode:1" Node="Node:1" />                            <PathNode key="PathNode:2" Node="Node:15" Distance="500" />                            <PathNode key="PathNode:3" Node="Node:13" Distance="320" />                            <PathNode key="PathNode:4" Node="Node:4" Distance="300" />                            <PathNode key="PathNode:5" Node="Node:14" Distance="450" />                        </PathNodes>                    </Path>                    <Path key="Path:2" IsEmpty="False">                        <PathNodes>                            <PathNode key="PathNode:5" Node="Node:14" />                            <PathNode key="PathNode:6" Node="Node:4" Distance="450" />                            <PathNode key="PathNode:7" Node="Node:13" Distance="300" />                            <PathNode key="PathNode:8" Node="Node:15" Distance="320" />                            <PathNode key="PathNode:9" Node="Node:1" Distance="500" />                        </PathNodes>                    </Path>                </Paths>               </Network>       </GLDocument>    </world></DocumentInfo>所以我需要讀取每個Path的每個PathNode,創建它們并將它們保存到數據庫,以及將當前 Path 的那些 PathNodes 保存在一個數組中,然后創建 Path 并能夠將數組分配給該 Path 的pathNodes屬性,如您所見,它是那些 PathNodes 的字符串數組。我的代碼是:讀取工作正常,所有 PathNodes 都被創建并保存在數據庫中就好了,問題是Path:1和Path:2都使用相同的 PathNodes 列表保存,Path:1有PathNodes的列表:2。所以這個Promise和.then在 outter Promise 內部無法正常工作,因為它正在讀取所有 PathNodes,然后才讀取 Paths,并將最后組裝的 PathNodes 數組分配給最后一個 Path。你能幫我解決這個問題嗎,所以每個Path都有相應的PathNodes?我試過從多個地方刪除異步,等待保存...謝謝。
查看完整描述

2 回答

?
MMTTMM

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


查看完整回答
反對 回復 2023-06-15
?
jeck貓

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);


        }))

...


查看完整回答
反對 回復 2023-06-15
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號