Firebase 无法从数据库中检索数据“db.collection is not a function"; [英] Firebase cannot retrieve data from database "db.collection is not a function"

查看:17
本文介绍了Firebase 无法从数据库中检索数据“db.collection is not a function";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法从 Firebase 数据库中获取任何内容.它显示以下错误:

Cannot get anything from the firebase database. It is showing an error of:

未捕获的类型错误:db.collection 不是函数

我在此处设置了脚本,如下所示:

I have the script setup here as shown:

var config = {
  apiKey: "*****",
  authDomain: "*****",
  databaseURL: "*****",
  projectId: "*****",
  storageBucket: "*****",
  messagingSenderId: "*****"
};
firebase.initializeApp(config);
const db = firebase.database();

const firstName = document.querySelector('#firstName').value;
const mainButton = document.querySelector('#mainButton');

mainButton.addEventListener('click', () => {
  db.collection("users").doc().set({
      first: firstName,
  })
  .then(function() {
      console.log("Document successfully written!");
  })
  .catch(function(error) {
      console.error("Error writing document: ", error);
  });
});

<input id="firstName" type="text" name="first-name" required>
<button id="mainButton" type="button">Submit</button>

我在这里遗漏了什么?

推荐答案

改变这个:

const db = firebase.database();

进入这个:

const db = firebase.firestore();

因为您使用的是 firestore 而不是实时数据库.

Since you are using firestore and not realtime database.

更多信息在这里:

https://firebase.google.com/docs/firestore/quickstart#initialize

这篇关于Firebase 无法从数据库中检索数据“db.collection is not a function";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆