简体   繁体   English

当我尝试读取 Firebase 上的数据时,出现“未捕获的 ReferenceError:firebase 未定义”错误

[英]I am getting "Uncaught ReferenceError: firebase is not defined" error when i try to read my datas on Firebase

I am getting the error in 43th code line.我在第 43 行代码中遇到错误。 Where am i doing wrong?我在哪里做错了? Also my database's image here: This is my database还有我的数据库图片:这是我的数据库

<script type="module">
  import {
    initializeApp
  } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-app.js";
  import {
    getDatabase,
    set,
    ref,
    update
  } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-database.js";
  import {
    getAuth,
    createUserWithEmailAndPassword,
    signInWithEmailAndPassword,
    onAuthStateChanged,
    signOut
  } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-auth.js";
  const firebaseConfig = {
    apiKey: ,
    authDomain: ,
    databaseURL: ,
    projectId: ,
    storageBucket: ,
    messagingSenderId: ,
    appId: ,
    measurementId:
  };
  const app = initializeApp(firebaseConfig);
  const db = getDatabase(app);
  const auth = getAuth();
  const firebaseRef = firebase.database().ref("Users");
  firebaseRef.once("value", function(snapshot) {
    snapshot.forEach(function(element) {
      console.log(element);
    })
  });
</script>

The firebase.database().ref() is the namespaced syntax (used on V8 and before) but you are using Modular SDK (V9+). firebase.database().ref()是命名空间语法(用于 V8 及之前的版本),但您使用的是模块化 SDK (V9+)。 Try refactoring the code as shown below using ref() and get() functions:尝试使用ref()get()函数重构代码,如下所示:

// ...imports
// import { get, ref } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-database.js"

const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
const auth = getAuth(app);

const firebaseRef = ref(db, 'Users');

get(firebaseRef).then((snapshot) => {
  if (snapshot.exists()) {
    console.log(snapshot.val());
  } else {
    console.log("No data available");
  }
}).catch((error) => {
  console.error(error);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误未捕获的 ReferenceError: firebase 未定义 - Getting error Uncaught ReferenceError: firebase is not defined 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property &#39;initializeApp&#39; of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 为什么会出现“ Uncaught ReferenceError:未定义userId”错误? - Why am I getting “Uncaught ReferenceError: userId is not defined” error? 使用打字稿时出现“ Uncaught ReferenceError:未定义角度” - I am getting “Uncaught ReferenceError: angular is not defined” when using typescript 当我尝试使用 emailjs 时出现 thsi 错误:Uncaught ReferenceError: sendMail is not defined emailjs - im getting thsi error when i try to use emailjs: Uncaught ReferenceError: sendMail is not defined emailjs 我在使用拆分方法时遇到错误Uncaught ReferenceError:函数未定义 - getting error when I am using split method Uncaught ReferenceError: function is not defined 未捕获的引用错误:firebase 未定义 - Uncaught referenceerror : firebase is not defined “未捕获的ReferenceError:未定义Firebase” - “Uncaught ReferenceError: Firebase is not defined” 未捕获的ReferenceError:未定义firebase - Uncaught ReferenceError: firebase is not defined 未捕获的引用错误:未定义 Firebase - Uncaught ReferenceError: Firebase is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM