I am having trouble with pushing, setting, and updating data on my Firebase Realtime Database. Below is the code I have been using. Any help would be greatly appreciated.
imports:
import { serverTimestamp, getDatabase, onChildAdded, push, set, update, runTransaction, get, ref as dbRef } from "firebase/database";
import { db } from "@/config/firebaseConfig";
In firebaseConfig, 'db' is simply a shorthand for getDatabase(app)
.
The set()
function is throwing an error.
async sendMessage() {
if (this.messageText.trim().length === 0) return;
const chatRef = dbRef(db, `chats/${this.chatId}`);
const snapshot = await get(chatRef);
const newMessageRef = await push(chatRef);
const newMessageKey = newMessageRef.key;
const newMessage = {
id: newMessageKey,
sender: this.currentUser.uid,
text: this.messageText.trim(),
timestamp: serverTimestamp(),
};
console.log("newMessageRef:", newMessageRef);
console.log("newMessage:", newMessage);
await set(newMessageRef, newMessage);
An error has occurred:
caught (in promise) TypeError: newChildren.insert is not a function at IndexMap.ts:147:30 at map (obj.ts:50:21) at Proxy.addToIndexes (IndexMap.ts:115:24) at Proxy.updateImmediateChild (ChildrenNode.ts:156:38) at Proxy.updateChild (ChildrenNode.ts:180:19) at viewProcessorApplyUserOverwrite (ViewProcessor.ts:486:34) at viewProcessorApplyOperation (ViewProcessor.ts:103:22) at viewApplyOperation (View.ts:213:18) at syncPointApplyOperation (SyncPoint.ts:107:9) at syncTreeApplyOperationHelper_ (SyncTree.ts:726:9) (
I have tried using both the old and new API. The read and write rules are set to true in my database configuration.