gpt4 book ai didi

Javascript - 无法实例化 firebase-firestore

转载 作者:行者123 更新时间:2023-12-03 07:17:07 27 4
gpt4 key购买 nike

试图关注谷歌Getting Started With Cloud Firestore on the Web - Firecasts

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

Uncaught Error: Cannot instantiate firebase-firestore - be sure to load firebase-app.js first.

但我相信我已经完成了所有应该做的事情。这有什么问题吗?

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)
});

})

最佳答案

要么导入整个 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

关于Javascript - 无法实例化 firebase-firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52751363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com