【问题标题】:How to call push() function from firebase database如何从 firebase 数据库调用 push() 函数
【发布时间】:2022-02-15 08:22:14
【问题描述】:

我正在尝试从 push 函数中获取唯一键,但每当我调用它时,我都会收到此错误:Uncaught TypeError: postsRef.push is not a function at HTMLFormElement。 这些是我的导入:

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";
    

这些是我用来调用推送函数的行:

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

希望大家能帮帮我,谢谢

【问题讨论】:

    标签: javascript firebase-realtime-database


    【解决方案1】:

    push 现在是顶级函数,不再是引用中的方法。

    由于您已经在导入push,您可以将其用作:

    const newPostRef = push(postsRef);
    

    这里的更改非常机械:从 object.operation() 更改为 operation(object),这是从旧版 Firebase SDK 迁移到 v9 及更高版本时的常见模式。

    【讨论】:

    • 解决了,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2021-02-10
    • 2020-12-18
    相关资源
    最近更新 更多