I'm using firebase's realtime database with react to create a CRUD, but the update function is making me very confused for two reasons.
1 - What is the most correct method to be used to do a simple update of one or more attributes? Sometimes I see using update(), other times updateDoc() other times I see set(). As a beginner in firebase (1st application I'm trying to do) I feel lost and confused with so many things, not to mention the difference between realtime and firestore that I can't quite understand why there are two databases inside one (!!?? )
2 - I'm trying to do the crud and as I said I'm not getting it to work. It's not giving any error on the console and I still get the success message, but the data doesn't update either on the screen or in the database, I can't understand which part I'm wrong. Something's missing? The code is:
useTransaction.tsx
async function updateTransaction(transaction: Transaction) {
await update(ref(realTimeDatabase, `transactions/${transaction.id}`), {
transactionUpdate
}).then(() => {
const updatedTransaction = transactions.map((transaction) => {
return transaction.id === transactionUpdate?.id ? transactionUpdate : transaction
});
setTransactions(updatedTransaction)
toast.success('Atualizada com sucesso')
console.log('Atualizada no firebase');
}).catch((err) => {
toast.error('Ocorreu um erro ao atualizar')
console.log(err);
})
}
UpdateTransactionModal.tsx
async function handleUpdateTransaction(event: FormEvent) {
event.preventDefault();
await updateTransaction({
id,
title,
amount,
category,
type,
createdAt: String(new Date()),
})
onRequestClose()
}
The repo is: https://github.com/eltonsantos/eurorcamento
UPDATE:
console.log("ID1: " + transactionUpdate?.id) >> undefined
console.log("ID2: " + transaction.id) >> -NJCBBEMVRMymMw-1tjI