Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
Generate jspdf auto table content from Firestore collection

I already have a report created in jspdf with an autotable in its place. It will be to list some data about a student which source is in a collection in Firestore. I have already made the reference to the collection with:

 const ref = collection (“students”).doc(“selectedStudent”).collection(“grades”)

In that collection there will be an undefined number of documents. Each one of them is an object with a lot of data that I already have in place:

enter image description here

From all that data, I would only use three fields to dynamically fill the table, so my table code would be as:

doc.autoTable({
  head: [['Subject', 'Teacher', 'Achievements']],
  body: [
    ['Subject1', 'Teacher1', 'Lorem ipsum'],
    ['Subject2', 'Teacher2', 'Lorem ipsum'],
    // ...
  ],
})

I know that in order to achieve it I should use a forEach function or loop going through the collection but my knowledge is limited and I don’t know how to get it done. This is what I have tried:

let keys = ['asignatura', 'authorApellidos', 'logros'];
let bodyData = [], i, j, rowData;

    for (i = 0; i < ref.length; i++) {
        rowData = [];
        for (j = 0; j < keys.length; ++j) {
          let key = keys [j];
          rowData.push (ref [i][key]);
        }
        bodyData.push (rowData);
    }

    doc.autoTable({
    head: [['Asignatura', 'Profesor', 'Descripción']],
    startY: 137,
    headStyles: { fillColor: [50, 205, 50] },
    body: bodyData
    })

However I have an empty table where I just see the head but no data.

When I do a console.log(ref) what I can see in the console is:

enter image description here

Maybe the data coming from Firebase needs to be formatted. I don't know.

I will appreciate any help. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you can use .map to transform each item in an array into an array with same length and a different format of data

example:

doc.autoTable({
  head: [['Subject', 'Teacher', 'Achievements']],
  body: ref.map(object => {
    return [object.subject, object.teacher, object.achievements];
  }),
});

or destructure the object as follows:

doc.autoTable({
  head: [["Subject", "Teacher", "Achievements"]],
  body: ref.map(({subject, teacher, achievements}) => { // <-- object destructuration
    return [subject, teacher, achievements];
  }),
});

since this map function is returning new format directly, you can skip the return and make a one-liner

doc.autoTable({
  head: [["Subject", "Teacher", "Achievements"]],
  body: ref.map(({subject, teacher, achievements}) => [subject, teacher, achievements]),
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda