【发布时间】:2019-01-24 17:18:38
【问题描述】:
我有以下可调用函数,它从我的数据库中获取一些数据,然后使用 html-pdf 使用该数据创建一个 PDF,将该 PDF 上传到存储,最后返回存储中文件的名称。 它以 https 形式运行良好,但我想将其转换为可调用函数,由于某种原因我无法弄清楚,它会因以下错误而崩溃: RangeError: maximum call stack size exceeded。
我怀疑这与 html-pdf 不适用于 Promise 而是使用错误/数据回调这一事实有关。但我试图将其转换为一个没有成功的承诺。
export const createPdf = functions.https.onCall((data, context) => {
const refId = data.refId;
const companyId = data.companyId;
const userId = context.auth.uid;
return admin.database().ref('/references').child(companyId).child(refId).once('value', (snapshot) => {
const filePath = '/references/' + refId + '/pdfs/' + refId + '.pdf';
const localeId = snapshot.child('locale').val();
return admin.database().ref('/tags').child(localeId).once('value', (tagsSnapshot) => {
const jsLocaleId = localeId.replace(/_/, "-");
const projectDate = moment().locale(jsLocaleId)
.year(snapshot.child('year').val())
.month(snapshot.child('month').val() - 1)
.date(15)
.format('MMMM YYYY');
const tags = tagsSnapshot.val();
const projectCategories = ...
const pictures = snapshot.child('pictures').val();
const pdfData = {
projectName: snapshot.child('projectName').val(),
surface: snapshot.child('surface').val(),
companyName: snapshot.child('companyName').val(),
date: projectDate,
newBuilding: snapshot.child('newBuilding').val(),
customerName: snapshot.child('customerName').val(),
categories: projectCategories,
address: snapshot.child('address').val().replace(/\n/g, '<br>'),
satellite: snapshot.child('satellite').val(),
pictures: !isNullOrUndefined(pictures) ? pictures.map((item) => {
return {url: item}
}) : []
};
console.log("data", pdfData);
const options = {...};
const localTemplate = path.join(os.tmpdir(), 'share.html');
const localPDFFile = path.join(os.tmpdir(), 'share.pdf');
const languageCode = localeId.split("_")[0];
return admin.storage().bucket().file('/templates/share-' + languageCode + '.html').download({destination: localTemplate}).then(() => {
const source = fs.readFileSync(localTemplate, 'utf8');
const html = handlebars.compile(source)(pdfData);
pdf.create(html, options).toFile(localPDFFile, function (err, result) {
if (err) {
console.log(err);
throw new functions.https.HttpsError('internal', err.message);
}
return admin.storage().bucket().upload(localPDFFile, {
destination: filePath,
resumable: false,
metadata: {contentType: 'application/pdf'}
}).then((files) => {
console.log("files", files);
return files[0].getMetadata().then((metadata) => {
const name = metadata[0]["name"];
return {
name: name
};
});
}).catch(error => {
console.error(error);
throw new functions.https.HttpsError('internal', "Could not upload PDF because " + error.message);
});
});
}).catch((error) => {
console.error("Could not download template");
throw new functions.https.HttpsError('internal', error.message);
});
});
});
});
【问题讨论】:
标签: typescript firebase google-cloud-functions