我已经从 firestore 集合中检索数据并将它们存储在一个数组中。我已经使用该数组将数据注入数据表,但是一旦加载数据表只显示一个条目,但我已经获取了 firestore 集合中的所有文档。可能是什么问题呢?下面是javascript代码和html
<table class="table table-striped table-bordered" style="width:fit-content; " id="mydatatable">
<thead>
<tr>
<th scope="col">Document id</th>
<th scope="col">title</th>
<th scope="col">details</th>
<th scope="col">timestamp</th>
<th scope="col">name</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
<tfoot>
<tr>
<th scope="col">Document id</th>
<th scope="col">title</th>
<th scope="col">details</th>
<th scope="col">timestamp</th>
<th scope="col">name</th>
</tr>
</tfoot>
</table>
var table = document.getElementById('tableBody');
var tableData = new Array();
db.collection("Emergency_Feeds").orderBy("timestamp", "desc").onSnapshot(function (querySnapshot) {
querySnapshot.docChanges().forEach(function (change) {
console.log('documents retrieved successfully');
console.log(`is here: ${change.doc.id} => ${change.doc.data().name}`);
var documentId = change.doc.id;
var username = change.doc.data().name;
var emTitle = change.doc.data().title;
var emDets = change.doc.data().details;
var emTimeDate = change.doc.data().timestamp.toDate();
if (change.type === "added") {
tableData = [
[
documentId,
emTimeDate,
emTitle,
emDets,
username
]
]
$('#mydatatable').DataTable({
retrieve: true,
data: tableData
});
}
if (change.type === "modified") {
tableData = [
[
documentId,
emTimeDate,
emTitle,
emDets,
username
]
]
$('#mydatatable').DataTable({
retrieve: true,
data: tableData
});
}
if (change.type === "removed") {
tableData = [
[
documentId,
emTimeDate,
emTitle,
emDets,
username
]
]
$('#mydatatable').DataTable({
retrieve: true,
data: tableData
});
}
});
});
