I have a method in nodejs used to generate a document. If I have two simultaneous requests, the content of one document is the same as the second one (the variable "contents" below). If for example the source.content retrieved by the first request should be "aaa" and the second is "bbb", the actual content of the file is "aaa" in both cases. If the requests are run sequentially, the content of the generated file is correct, "aaa" for the first request and "bbb" for the second.
public async generateDocument(req: Request, res: Response) {
const sourceId = parseInt(req.params.id, 10);
const source: ISource = await this.db.models.MoM.scope("full").findOne({ where: { sourceId : sourceId } });
const signatureDate = getDateNow();
// Load the docx file as a binary
const content = fs.readFileSync(path.resolve(__dirname, "../../../../assets/Template.docx"), "binary");
const zip = new PizZip(content);
const doc = new Docxtemplater();
doc.loadZip(zip);
doc.setData({
// @ts-ignore TODO: fix generic interfaces
writer_name: "" + source.writer.displayedName,
contents: source.content.split("\n").map((paragraph: string) => ({
paragraph,
})),
});
doc.render();
const buf = doc.getZip().generate({ type: "nodebuffer" });
const outputFilePath = path.resolve(__dirname, "../../../../assets/output.docx");
fs.writeFileSync(outputFilePath, buf);
}