我对异步请求有疑问。最后的承诺没有解决。 因此,我认为问题可能出在“探针”内部情况上。 我做了一个异步/等待,我的代码的所有部分开始同步,但是最后一个Promise不能在这里解决-(packer.toBuffer(doc)); 我也试图重写Promise []这部分-
await probe(urlObj.url).then(async result => {
const scale = result.width / DEFAULT_WIDTH_DOCUMENT;
await doc.createImage(
await requestPromise(urlObj.url),
result.width,
result.height
);
// image.scale(scale);
});
function createDoc(project, company, messages, photoUrls) {
const contentAddress = (company.street || '') + ' ' + (company.zipcode || '') + ' ' + (company.city || '');
const contentContact = (company.email || '') + ' ' + (company.phone || '');
const companyAddress = contentAddress;
const companyContact = contentContact;
const DEFAULT_WIDTH_DOCUMENT = 793.92;
const dateStr = moment().format('YYYY-MM-DD HH:mm:ss');
const fileName = project.name + '_' + dateStr + '.docx';
const blob = bucket.file(fileName);
const blobStream = blob.createWriteStream();
let imagesCount = 0;
let messagesCount = 0;
let documentsCount = 0;
console.log('fileName =>', fileName);
console.log('messages => ', messages);
console.log('company => ', company);
console.log('project => ', project);
console.log('photoUrls => ', photoUrls);
const fileIcon = bucket.file('Group.png');
const validDate = moment().add(30, 'days').format('YYYY-MM-DD').toString();
return fileIcon.getSignedUrl({
action: 'read',
expires: validDate
}).then(signedUrls => {
// signedUrls[0] contains the file's public URL
console.log('image: ', signedUrls[0]);
const iconUrl = signedUrls[0];
messages.forEach((message) => {
if (message.messageType === 'IMAGE') {
imagesCount++;
return;
}
if (message.messageType === 'DOCUMENT') {
documentsCount++;
return;
}
messagesCount++;
});
const doc = new Document();
const currentPageRun = new TextRun("Seite: ").size(22).bold().tab().tab().tab().tab().tab().tab().tab().tab().tab().tab().pageNumber();
const lastPage = new TextRun("/").size(22).bold().numberOfTotalPages();
doc.Footer.createParagraph()
.addRun(new TextRun(company.name).size(22).bold())
.addRun(currentPageRun)
.addRun(lastPage);
doc.Styles.createParagraphStyle('Heading2', 'Heading 2')
.basedOn("Normal")
.next("Normal")
.color('000000')
.quickFormat()
.size(28) // 26 half-points === 13pt font
.bold()
.spacing({before: 240, after: 80}); // TWIP for both
doc.Styles.createParagraphStyle('Heading3', 'Heading 3')
.basedOn("Normal")
.next("Normal")
.color('FF5C20')
.quickFormat()
.size(22) // 26 half-points === 13pt font
.bold()
.spacing({before: 240, after: 120}); // TWIP for both
const borderHeader = new Paragraph().createBorder();
borderHeader.Borders.addTopBorder('#000000', "1", "single", "20");
const borderParagraph = new Paragraph().createBorder();
borderParagraph.Borders.addBottomBorder('#d8d8d8', "1", "single", "30");
doc.Styles.createParagraphStyle('msgTitle', 'msgTitle')
.basedOn("Normal")
.next("Normal")
.color('727272')
.quickFormat()
.size(20) // 26 half-points === 13pt font
.bold()
.spacing({before: 240, after: 10}); // TWIP for both
doc.Styles.createParagraphStyle('msgContent', 'msgContent')
.basedOn("Normal")
.next("Normal")
.color('000000')
.quickFormat()
.size(24); // 26 half-points === 13pt font
const paragraph0 = new Paragraph();
const paragraph011 = new Paragraph();
const institutionText0 = new TextRun(' Craftnote').allCaps().bold();
const institutionText01 = new TextRun(' #Handwerkerapp').allCaps().bold();
const countText01 = new TextRun(` www.mycraftnote.de`).allCaps().tab().tab().tab();
const countText02 = new TextRun(` Verbindet Deine Baustelle mit dem Büro`).allCaps().tab();
paragraph0.addRun(institutionText0);
paragraph0.addRun(countText01);
paragraph0.title().heading2().justified().style('Heading2');
paragraph011.addRun(institutionText01);
paragraph011.addRun(countText02);
paragraph011.title().heading2().style('Heading3');
const paragraph = new Paragraph();
const paragraph11 = new Paragraph();
const paragraph111 = new Paragraph();
const institutionText = new TextRun(project.name.toUpperCase()).bold();
const institutionText1 = new TextRun(contentAddress.trim().length > 0 ? companyAddress : '').bold();
const institutionText2 = new TextRun(contentContact.trim().length > 0 ? companyContact : '').bold();
const countText1 = new TextRun(`${imagesCount} Bilder`).tab().tab().tab().tab().tab();
const countText2 = new TextRun(`${messagesCount} Chat-Nachrichten`).tab().tab().tab().tab();
const countText3 = new TextRun(`${documentsCount} PDF-Dateien`).tab().tab().tab().tab();
paragraph.addRun(institutionText);
paragraph.addRun(countText1);
paragraph11.addRun(institutionText1);
paragraph11.addRun(countText2);
paragraph111.addRun(institutionText2);
paragraph111.addRun(countText3);
doc.createImage(request(iconUrl), 50, 50, {
floating: {
horizontalPosition: {
offset: 900000,
},
verticalPosition: {
offset: 1234800,
},
wrap: {
type: TextWrappingType.SQUARE,
side: TextWrappingSide.BOTH_SIDES,
},
margins: {
right: 200,
},
},
});
doc.addParagraph(paragraph0);
doc.addParagraph(paragraph011);
doc.addParagraph(borderHeader);
doc.addParagraph(paragraph);
doc.addParagraph(paragraph11);
doc.addParagraph(paragraph111);
doc.addParagraph(borderParagraph);
const paragraph2 = new Paragraph();
const chatTitle = new TextRun("CHAT NACHRICHTEN").break().bold();
paragraph2.addRun(chatTitle);
paragraph2.title().heading2().style('Heading2');
doc.addParagraph(paragraph2);
const hyperlinks = [];
const promise = new Promise(async resolve => {
for (const message of messages) {
const msgDate = Math.floor(+new Date(message.timestamp * 1000));
const msgDateStr = moment(msgDate)
.utcOffset("+0100")
.format("YYYY-MM-DD HH:mm:ss");
const paragraphMsgAuthor = new Paragraph();
const msgAuthor = new TextRun(`${message.author} - ${msgDateStr}`)
.break()
.bold();
paragraphMsgAuthor.addRun(msgAuthor);
paragraphMsgAuthor.style("msgTitle");
doc.addParagraph(paragraphMsgAuthor);
if (message.messageType === "IMAGE") {
const urlObj = photoUrls.find(urlItem => {
return message.id === urlItem.messageId;
});
await probe(urlObj.url).then(async result => {
const scale = result.width / DEFAULT_WIDTH_DOCUMENT;
await doc.createImage(
await requestPromise(urlObj.url),
result.width,
result.height
);
// image.scale(scale);
});
}
if (message.messageType === "DOCUMENT") {
const urlObj = photoUrls.find(urlItem => {
return message.id === urlItem.messageId;
});
const paragraphDocMsg = new Paragraph();
const msgDoc = new TextRun(`${message.content} `);
paragraphDocMsg.addRun(msgDoc);
paragraphDocMsg.style("msgContent");
hyperlinks.push({
paragraph: paragraphDocMsg,
link: urlObj.url
});
doc.addParagraph(paragraphDocMsg);
}
const paragraphMsg = new Paragraph();
const msg = new TextRun(message.content).break();
paragraphMsg.addRun(msg);
paragraphMsg.style("msgContent");
doc.addParagraph(paragraphMsg);
}
resolve();
});
return promise.then(() => {
console.log('AFTER RESOLVE IMG PROMISE');
hyperlinks.forEach((hyperlink) => {
const paragraphDocMsg = hyperlink.paragraph;
paragraphDocMsg.addHyperLink(doc.createHyperlink(hyperlink.link, 'Download'));
});
console.log('AFTER PROMISE RETURN');
console.log('doc', doc);
const packer = new Packer();
return packer.toBuffer(doc)
.then(buffer => {
console.log(buffer, 'Buffer')
})
.catch(e => console.log('ERROR', e));
})
});
}