Home > Software design >  How to call push() function from firebase database
How to call push() function from firebase database

Time:02-15

I'm trying to get a unique key from the push function but whenever I call it I get this error: Uncaught TypeError: postsRef.push is not a function at HTMLFormElement. These are my imports:

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js";
import { getDatabase, set, ref, update, onValue, remove, push } from "https://www.gstatic.com/firebasejs/9.6.6/firebase-database.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signOut } from "https://www.gstatic.com/firebasejs/9.6.6/firebase-auth.js";
    

And these are the lines that I use to call the push function:

const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
const postsRef = ref(db, 'devices/');
const newPostRef = postsRef.push();

I hope you guys can help me, thank you

CodePudding user response:

push is nowadays a top-level function, and no longer a method in a reference.

Since you're already importing push, you can use it as:

const newPostRef = push(postsRef);

The change is very mechanical here: changing from object.operation() to operation(object), which is a common pattern when moving over from older Firebase SDK to v9 and above.

  • Related