[英]Firestore onSnapshot - firebase.firestore is not a function - Failed to resolve module specifier “firebase”
I'm just trying to learn firebase, and I wanted to use onSnapshot so switched from "realtime database" to "firestore".我只是想学习 firebase,我想使用 onSnapshot 所以从“实时数据库”切换到“firestore”。 After switching I copied this boilerplate code:
切换后,我复制了这个样板代码:
document.addEventListener("DOMContentLoaded", evt=> {
const app = firebase.app();
const db = firebase.firestore();
db.collection('users')
.onSnapshot((snapshot) => {
console.log(`Received doc snapshot: ${snapshot}`);
}, (error) => {
console.log(`Encountered error: ${error}`);
});
});
However I got that "firebase.firestore is not a function".但是我得到了“firebase.firestore 不是函数”。 I found the solution here: https://stackoverflow.com/a/50684682/4907950 which is just adding:
我在这里找到了解决方案: https://stackoverflow.com/a/50684682/4907950这只是添加:
import * as firebase from 'firebase';
import 'firebase/firestore';
However, then I get: "Uncaught TypeError: Failed to resolve module specifier "firebase". Relative references must start with either "/", "./", or "../"."但是,然后我得到:“未捕获的类型错误:无法解析模块说明符“firebase”。相对引用必须以“/”、“./”或“../”开头。”
EDIT: Also worth noting that I've added <script defer src="/__/firebase/7.14.2/firebase-firestore.js"></script>
to my HTML file and the error persists.编辑:还值得注意的是,我已将
<script defer src="/__/firebase/7.14.2/firebase-firestore.js"></script>
添加到我的 HTML 文件中,并且错误仍然存在。
Removing:删除:
import * as firebase from 'firebase';
import 'firebase/firestore';
From the stackoverflow post I had found, while keeping the firestore link seemed to do the trick.从我发现的stackoverflow帖子中,保留firestore链接似乎可以解决问题。
For some reason I had to use the complete URL and keep them at top of <BODY>
tag like this:出于某种原因,我不得不使用完整的 URL 并将它们放在
<BODY>
标记的顶部,如下所示:
<body>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-storage.js"></script>
<script src="app.js"></script>
</body>
Also I had to re-install all requires stuff using Firebase CLI.我还必须使用 Firebase CLI 重新安装所有需要的东西。 It worked perfectly thereafter.
此后它工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.