skip to Main Content

How can I add only one element to the page that I have added? Now, every time I add a bank, the entire database is pulled out and those banks that have already been added are drawn again.

const sendBankData = () => {
  set(ref(database, "banks/" + bankName.value), {
    bankName: bankName.value,
    interestRate: interestRate.value,
    maximumLoan: maximumLoan.value,
    minimumDownPayment: minimumDownPayment.value,
    loanTerm: loanTerm.value,
  })
    .then(() => {
      successfulPopUp();
    })
    .catch((error) => {
      // errorPopUp();
    });
};
const getBanksFunc = () => {
  const bankRef = ref(database, "banks/");
  onValue(bankRef, (snapshot) => {
    const data = snapshot.val();
    displayBanks(data);
  });
};

2

Answers


  1. Haven’t used Firebase in a long time but if I remember correctly you’d pass the ID of the bank into the ref
    (const bankRef = ref(database, "banks/ID-OF-THE-BANK");

    or you could sort and limit by create date.

    Login or Signup to reply.
  2. As commented above mentioned.
    You need to pass the ID to get the document you just inserted.

    So for that, you have two options:

    One is to set a custom document Id and get the id you just inserted.
    Or get the doc id with the reference:

    But for Firestore realtime

    const sendBankData = () => {
      set(ref(database, "banks/" + bankName.value), {
        bankName: bankName.value,
        interestRate: interestRate.value,
        maximumLoan: maximumLoan.value,
        minimumDownPayment: minimumDownPayment.value,
        loanTerm: loanTerm.value,
      }).catch((error) => {
          // errorPopUp();
        });
    };
    
    /* You should be able to get the key from the just inserted document using this: */
    const sendBankData_key = sendBankData.key
    

    /* Then you should be able to push it, also accordingly the doc, you can insert a blank document, get the key and then update it. Also, you can use transactions to update several times inserted documents and avoid inconsistency. */

    const getBanksFunc = () => {
    const bankRef = ref(database, "banks/${sendBankData_key}");
    onValue(bankRef, (snapshot) => {
      const data = snapshot.val();
      displayBanks(data);
     });
    

    };

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search