I actually process the template twice - once setting withRegions = false and once setting withRegions = true. I took the 21.12.0 update, and it did not fix the issue - even using the template you provided.
That said - wrapping the entire document in a foreach is not an option, as it makes no sense in this context - the data is a single record.
Here is the code I am running for reference:
const wordsApi = new WordsApi(clientId, secret);
let requestTemplatePath = "templates/FixedCoverageChangeTemplate.docx";
let requestDataPath = "data/coverageChange.json";
let outputPath = "output/coverageChange.pdf";
const mailMergeRequest = new ExecuteMailMergeOnlineRequest({
template: fs.createReadStream(requestTemplatePath),
data: fs.createReadStream(requestDataPath),
cleanup: "EmptyParagraphs,UnusedRegions,UnusedFields,RemoveTitleRow,RemoveTitleRowInInnerTables",
withRegions: false
});
wordsApi.executeMailMergeOnline(mailMergeRequest)
.then((mailMergeRequestResult) => {
console.log("merge complete");
const regionMergeRequest = new ExecuteMailMergeOnlineRequest({
template: Readable.from(mailMergeRequestResult.body),
data: fs.createReadStream(requestDataPath),
cleanup: "EmptyParagraphs,UnusedRegions,UnusedFields,RemoveTitleRow,RemoveTitleRowInInnerTables",
withRegions: true //have to process twice if template contains enumerable data :( https://forum.aspose.cloud/t/mail-merge-word-document-from-xml-data-in-node-js-basics/8247/14
});
wordsApi.executeMailMergeOnline(regionMergeRequest)
.then((regionMergeRequestResult) => {
console.log("region merge complete");
const convertRequest = new ConvertDocumentRequest({
document: Readable.from(regionMergeRequestResult.body),
format: "pdf"
});
wordsApi.convertDocument(convertRequest)
.then((convertRequestResult) => {
console.log("convert complete");
fs.writeFile(outputPath, convertRequestResult.body, function (err) {
if (err) return console.log(err);
console.log('file saved');
});
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});