Javascript - 无法实例化firebase-firestore

问题描述 投票:0回答:2

试图跟随谷歌Getting Started With Cloud Firestore on the Web - Firecasts

出于某种原因,我收到此错误:

未捕获错误:无法实例化firebase-firestore - 请务必先加载firebase-app.js。

但我相信我已经完成了所有工作。这有什么问题?

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://www.gstatic.com/firebasejs/5.5.3/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.5.3/firebase-firestore.js"></script>

</head>
<body>

<h1 id="hotDogOutput">Hot dog status:</h1>
<input type="textfield" id="latestHotDogStatus">
<button id="saveButton">Save</button>
<script src="./app.js"></script>

</body>
</html>

使用Javascript

// Initialize Firebase
var config = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "..."
};
firebase.initializeApp(config);

const firestore = firebase.firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
firestore.settings(settings);

const docRef = firestore.doc("samples/sandwichData");

const outputHeader = document.querySelector("#hotDogOutput");
const inputTextField = document.querySelector("#latestHotDogStatus");
const saveButton = document.querySelector("#saveButton");

saveButton.addEventListener("click", function () {
    const textToSave = inputTextField.value;
    console.log("I am going to save " + textToSave + " to Firestore");
    docRef.set({
       hotDogStatus: textToSave
    }).then(function() {
        console.log("Status Saved!");
    }).catch(function(error) {
        console.log("Got an error: ", error)
    });

})
javascript html firebase google-cloud-firestore
2个回答
3
投票

要么输入整个firebase.js,要么只输入firebase-app.js,然后输入firebase-firestore.js。这里发生的是firebase-firestore.js期望firebase-app.js(核心Firebase客户端)而不是整个库。

<script src="https://www.gstatic.com/firebasejs/5.5.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.3/firebase-firestore.js"></script>

有用的指南:https://firebase.google.com/docs/web/setup


0
投票

如果您使用的是实时数据库,则只添加以下两个脚本srcs:

<script src="https://www.gstatic.com/firebasejs/5.9.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.3/firebase-database.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.