[英]How to work with promise with firestore + pdfmake in Firebase Functions
I'm trying to do a pdf file getting data from firestore using firebase functions, but now I'm getting a problem. 我正在尝试制作一个使用Firebase函数从Firestore获取数据的pdf文件,但现在遇到了问题。 I have this data structure: 我有这个数据结构:
I need to convert this struture in a json to show on pdfMake. 我需要将此结构转换为json才能显示在pdfMake上。 I know that I have to use promises, but the final object doesnt appear like I expect. 我知道我必须使用Promise,但是最终对象却不会像我期望的那样出现。 When I use the first layer (only field from de firsts docs), works fine, but when I try to enter these same Doc's Collection, i receive a error. 当我使用第一层(仅来自de firsts docs中的字段)时,工作正常,但是当我尝试输入这些相同的Doc's Collection时,出现错误。 I'll put the code below. 我将代码放在下面。
The error is that my 'second then' return before than first "then" be resolved, so my PDF File render a blank file on the screen. 错误是我的“第二个然后”比第一个“然后”得到了返回,因此我的PDF文件在屏幕上呈现了一个空白文件。 It doesnt show any error, but on console I can see that data is fetched after the response be returned. 它没有显示任何错误,但是在控制台上,我可以看到返回响应后已获取数据。 I need to resolve the firsts then included on the loop before send any response, but it doesnt happen 在发送任何响应之前,我需要先解析第一个然后包含在循环中,但是这不会发生
exports.helloPDF = functions.https.onRequest( (request, response) => {
try {
db.collection('lista_substituicao').get().then((lista_substituicao) =>{
let docDefinition = { //definitions will be used on pdf file
background: function () {
return [
{
image: 'bg.jpg',
width: 550
}
];
},
content: []
};
lista_substituicao.forEach( grupo => {
let grupoDef = {id: grupo.id, ...grupo.data()}
let tabelaGrupo = {
table: {
widths: ['*'],
body: [
[`Grupo ${grupoDef.grupo}`]
]
}
}
db.collection(`lista_substituicao/${grupoDef.id}/alimentos/`).get().then(alimentos =>{
let alimentosTable = {
table: {
widths: ['*'],
body: [
["Alimento"]
]
}
}
alimentos.forEach(alimentoDef =>{
let alimento = {id: alimentoDef.id, ...alimentoDef.data()}
alimentosTable['table']['body'].push([alimento.alimento])
})
tabelaGrupo['table']['body'].push(alimentosTable)
docDefinition['content'].push(tabelaGrupo)
})
})
return docDefinition
}).then((finalDocDefinition) =>{
console.log(finalDocDefinition) // i get undefined here
let doc = printer.createPdfKitDocument(docDefinition);
response.setHeader('Content-Type', 'application/pdf');
doc.end();
return doc.pipe(response)
})
} catch (error) {
console.log(error)
}
})
I changed the code and now its working as expected. 我更改了代码,现在可以按预期工作。
I did another functions whats return a json tree as I wanted. 我做了另一个函数,是什么返回我想要的json树。 The code: 编码:
db.collection(`lista_substituicao`).get().then(lista_substituicao =>{
let grupos = []
let grupoPromisses = []
lista_substituicao.forEach(grupo => {
let grupoData = {id: grupo.id, ...grupo.data(), alimentos: []}
let promise = db.collection(`lista_substituicao/${grupo.id}/alimentos/`).get()
grupoPromisses.push(promise)
grupos[grupo.id] = grupoData
})
Promise.all(grupoPromisses).then(alimentos => {
alimentos.forEach(alimentos =>{
alimentos.forEach(alimento =>{
let idGrupo = alimento.ref.parent.parent.id
grupos[idGrupo]['alimentos'].push(alimento.data())
})
})
return response.status(201).json({...grupos});
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.