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

所以,我正在阅读这个 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?我试过从多个地方删除异步,等待保存...谢谢。


侃侃尔雅
浏览 134回答 2
2回答

MMTTMM

您的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

jeck猫

尝试添加return之前repository.savePathNode(newPathNode)。现在,它接缝了,你的嵌套承诺没有被等待...await Promise.all(path.PathNodes.PathNode.map(async pathNode => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newPathNode = new PathNode();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPathNode.key = pathNode._attributes.key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPathNode.node = pathNode._attributes.Node;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPathNode.duration = pathNode._attributes.Duration;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPathNode.distance = pathNode._attributes.Distance;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pathNodesArray.push(newPathNode.key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return repository.savePathNode(newPathNode);&nbsp; &nbsp; &nbsp; &nbsp; }))...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript