亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Firebase 的數據位置

Firebase 的數據位置

森欄 2023-03-10 16:04:12
我試圖保持我從 firebase 獲得的數據的位置但是當我刷新頁面時它顯示在不同的位置而不是他創建的位置我想使用 'position' 和 'beforeend' 但結果是一樣的例如我發布 (a,b,c,d,e) 并且當我刷新頁面時它顯示 (e,d,a,b,c) here some pictures enter image description hereconst addForm = document.querySelector(".add");const list = document.querySelector(".todos");const search = document.querySelector(".search input");// generate new toDo'sconst generateTemplate = (toDo, id) => {  let html = ` <li data-id=${id} class="list-group-item d-flex justify-content-between align-items-center">    <span>${toDo.title}</span><i class="far fa-trash-alt delete"></i>  </li>`;  const position = "beforeend";  list.insertAdjacentHTML(position, html);};const deleteTodo = (id) => {  const todos = document.querySelectorAll("li");  todos.forEach((toDo) => {    if (toDo.getAttribute("data-id") === id) {      toDo.remove();    }  });};// get the info in the pagedb.collection("Todos").onSnapshot((snapshot) => {  snapshot.docChanges().forEach((change) => {    const doc = change.doc;    if (change.type === "added") {      generateTemplate(doc.data(), doc.id);    } else if (change.type === "removed") {      deleteTodo(doc.id);    }  });});// submit the todoaddForm.addEventListener("submit", (e) => {  e.preventDefault();  const now = new Date();  const toDo = {    title: addForm.add.value.trim(),    created_at: firebase.firestore.Timestamp.fromDate(now),  };  db.collection("Todos")    .add(toDo)    .then(() => {      console.log("todo added");    })    .catch((err) => {      console.log(err);    });  addForm.reset();});// delete todo'slist.addEventListener("click", (e) => {  if (e.target.classList.contains("delete")) {    const id = e.target.parentElement.getAttribute("data-id");    db.collection("Todos")      .doc(id)      .delete()      .then(() => {        console.log("Todo Deleted");      });  }});
查看完整描述

1 回答

?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

您需要使用orderByfetch documents with sorting。
它應該看起來像這樣。

db.collection("Todos")
  .orderBy('created_at')
  .get()
  .then((snapshot) => {
    snapshot.docs.forEach((doc) => console.log(doc.data()));
  });


查看完整回答
反對 回復 2023-03-10
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號