Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call push() function from firebase database

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading