【发布时间】:2021-03-08 10:00:55
【问题描述】:
所以,我正在阅读这个 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>
路径是具有这种格式的模式:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Path = new Schema({
key: {type: String, unique: true},
isEmpty: Boolean,
pathNodes: [String]
});
module.exports = mongoose.model('Path', Path);
PathNode 是一个具有这种格式的模式:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var PathNode = new Schema({
key: String,
node: String,
distance: Number
});
module.exports = mongoose.model('PathNode', PathNode);
所以我需要读取每个Path的每个PathNode,创建它们并将它们保存到数据库,以及将当前Path的那些PathNodes保存在一个数组中,然后创建路径并能够将数组分配给该路径的 pathNodes 属性,如您所见,该属性是这些 PathNodes 的字符串数组。 我的代码是:
var PathNode = require('../models/pathNode');
var Path = require('../models/path');
var repository = require('../../repository');
var jsonConverted = JSON.parse(convert.xml2json(req.file.buffer.toString(), { compact: true, spaces: 4, alwaysChildren: true }));
var pathNodesArray = [];
await Promise.all(jsonConverted.GlDocumentInfo.world.GlDocument.GlDocumentNetwork.Network.Paths.Path.map(async path => {
pathNodesArray = [];
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);
repository.savePathNode(newPathNode);
})).then(async () => {
var newPath = new Path();
newPath.key = path._attributes.key;
newPath.isEmpty = path._attributes.IsEmpty.toString().toLowerCase();
Object.assign(newPath.pathNodes, pathNodesArray);
repository.savePath(newPath);
});
})).then(async () => {
(...) //doesn't matter for this question
}
读取工作正常,所有 PathNode 都被创建并保存在数据库中,问题是 Path:1 和 Path:2 都被保存了使用相同的 PathNodes 列表,Path:1 具有来自 Path:2 的 PathNodes 列表。因此,外部 Promise 中的 Promise 和 .then 无法正常工作,因为它正在读取所有 PathNode,然后才读取 Path,并分配最后组装的数组PathNodes 到最后一个 Path。
你能帮我解决这个问题,所以每个 Path 都有其对应的 PathNodes 吗?我试过从多个地方删除 async,await 保存... 谢谢。
【问题讨论】:
-
不要使用
.then(async () => {。你已经在await执行承诺,只需将代码放在await语句之后,而不是在then回调中。 -
您使用了非常混乱的
async/await和then()组合。如果你只是坚持一个或另一个,这将更容易阅读 -
@Bergi 我就是这样做的,但是,它在两个路径中保存了相同的路径节点。绝望在这一点上是真实的:c
标签: javascript node.js promise async-await es6-promise