使用 PDFKit 和 GCF 忽略已完成函数的异常
Ignoring exception from a finished function using PDFKit & GCF
所以目前我正在尝试从我的 Firebase 数据库中的大量数据生成 PDF(使用 Node.js、PDFKit 和 GCF)。其中一项是图像 URL,但 PDF 工具包需要 base64 格式的图像。目前正在创建 PDF,但我收到了大约 6 个 "Ignoring exception from a finished function".
的调用
我认为这与 base64 函数有关,但我似乎无法确定问题所在...该函数在触发 onCreate 后返回其结果,但我认为 "await"是为了阻止这个?有什么帮助/想法吗?
exports.onAppComplete = functions.database.ref("users/{appId}").onCreate((snapshot, context) =>{
var appId = context.params.appId;
var appDb = admin.database().ref(`Applications/${appId}`);
return Promise.all([appDb.once('value'),appId]).then(([applicationDb, appId])=>{
var application = {
appId:appId,
dogId:applicationDb.child("dogId").val(),
userId: applicationDb.child("appWith").val(),
};
const Logo = admin.database().ref(`users/logouri`);
return Promise.all([Logo.once('value')]).then(([Logo])=>{
const PDFDocument = require('pdfkit');
let doc = new PDFDocument({ margin: 72 });
doc.size = [595.28, 841.89];
doc
.image('./images/logo.png', 50, 45, { width: 100 })
.fillColor("#444444")
.moveDown(1);
var logoUri = Logo.val();
console.log("pictureURL",logoUri);
toBase64(logoUri,50,50,doc);
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
})
});
});
function toBase64(url, x, y, pdfdoc) {
return new Promise(async (resolve, reject)=>{
var https = require('https');
var req = await https.get(url, (res) => {
res.setEncoding('base64');
let body = "data:" + res.headers["content-type"] + ";base64,";
console.log("BODY", body)
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
console.log("END", body)
pdfdoc.image(body, x, y);
resolve(res);
});
});
req.on('error', err => {
console.error('error!');
reject(err);
});
});
}
我正在使用 Node.js 8,并且有一个单独的徽标在本地加载。
在 doug 的一些提示之后...这是回答这个问题的代码,事实证明 Promise.all 是关键:
exports.onAppComplete = functions.database.ref("users/{appId}").onCreate((snapshot, context) =>{
var appId = context.params.appId;
var appDb = admin.database().ref(`Applications/${appId}`);
return Promise.all([appDb.once('value'),appId]).then(([applicationDb, appId])=>{
var application = {
appId:appId,
dogId:applicationDb.child("dogId").val(),
userId: applicationDb.child("appWith").val(),
};
const Logo = admin.database().ref(`users/logouri`);
return Promise.all([Logo.once('value')]).then(([Logo])=>{
const PDFDocument = require('pdfkit');
let doc = new PDFDocument({ margin: 72 });
doc.size = [595.28, 841.89];
doc
.image('./images/logo.png', 50, 45, { width: 100 })
.fillColor("#444444")
.moveDown(1);
var logoUri = Logo.val();
console.log("pictureURL",logoUri);
return Promise.all([toBase64(logoUri),doc,application]).then(([logoURIBase64, doc, application])=>{
doc.rect(0,0,590,70,40).fillAndStroke("#339999");
doc.image(logoURIBase64,50,50,{width:100});
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
});
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
})
});
});
function toBase64(url) {
return new Promise(async (resolve, reject)=>{
var https = require('https');
var req = await https.get(url, (res) => {
res.setEncoding('base64');
let body = "data:" + res.headers["content-type"] + ";base64,";
console.log("BODY", body)
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
console.log("END", body)
pdfdoc.image(body, x, y);
resolve(body);
});
});
req.on('error', err => {
console.error('error!');
reject(err);
});
});
}
所以目前我正在尝试从我的 Firebase 数据库中的大量数据生成 PDF(使用 Node.js、PDFKit 和 GCF)。其中一项是图像 URL,但 PDF 工具包需要 base64 格式的图像。目前正在创建 PDF,但我收到了大约 6 个 "Ignoring exception from a finished function".
的调用我认为这与 base64 函数有关,但我似乎无法确定问题所在...该函数在触发 onCreate 后返回其结果,但我认为 "await"是为了阻止这个?有什么帮助/想法吗?
exports.onAppComplete = functions.database.ref("users/{appId}").onCreate((snapshot, context) =>{
var appId = context.params.appId;
var appDb = admin.database().ref(`Applications/${appId}`);
return Promise.all([appDb.once('value'),appId]).then(([applicationDb, appId])=>{
var application = {
appId:appId,
dogId:applicationDb.child("dogId").val(),
userId: applicationDb.child("appWith").val(),
};
const Logo = admin.database().ref(`users/logouri`);
return Promise.all([Logo.once('value')]).then(([Logo])=>{
const PDFDocument = require('pdfkit');
let doc = new PDFDocument({ margin: 72 });
doc.size = [595.28, 841.89];
doc
.image('./images/logo.png', 50, 45, { width: 100 })
.fillColor("#444444")
.moveDown(1);
var logoUri = Logo.val();
console.log("pictureURL",logoUri);
toBase64(logoUri,50,50,doc);
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
})
});
});
function toBase64(url, x, y, pdfdoc) {
return new Promise(async (resolve, reject)=>{
var https = require('https');
var req = await https.get(url, (res) => {
res.setEncoding('base64');
let body = "data:" + res.headers["content-type"] + ";base64,";
console.log("BODY", body)
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
console.log("END", body)
pdfdoc.image(body, x, y);
resolve(res);
});
});
req.on('error', err => {
console.error('error!');
reject(err);
});
});
}
我正在使用 Node.js 8,并且有一个单独的徽标在本地加载。
在 doug 的一些提示之后...这是回答这个问题的代码,事实证明 Promise.all 是关键:
exports.onAppComplete = functions.database.ref("users/{appId}").onCreate((snapshot, context) =>{
var appId = context.params.appId;
var appDb = admin.database().ref(`Applications/${appId}`);
return Promise.all([appDb.once('value'),appId]).then(([applicationDb, appId])=>{
var application = {
appId:appId,
dogId:applicationDb.child("dogId").val(),
userId: applicationDb.child("appWith").val(),
};
const Logo = admin.database().ref(`users/logouri`);
return Promise.all([Logo.once('value')]).then(([Logo])=>{
const PDFDocument = require('pdfkit');
let doc = new PDFDocument({ margin: 72 });
doc.size = [595.28, 841.89];
doc
.image('./images/logo.png', 50, 45, { width: 100 })
.fillColor("#444444")
.moveDown(1);
var logoUri = Logo.val();
console.log("pictureURL",logoUri);
return Promise.all([toBase64(logoUri),doc,application]).then(([logoURIBase64, doc, application])=>{
doc.rect(0,0,590,70,40).fillAndStroke("#339999");
doc.image(logoURIBase64,50,50,{width:100});
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
});
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
})
});
});
function toBase64(url) {
return new Promise(async (resolve, reject)=>{
var https = require('https');
var req = await https.get(url, (res) => {
res.setEncoding('base64');
let body = "data:" + res.headers["content-type"] + ";base64,";
console.log("BODY", body)
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
console.log("END", body)
pdfdoc.image(body, x, y);
resolve(body);
});
});
req.on('error', err => {
console.error('error!');
reject(err);
});
});
}