Firestore onSnapshot-firebase.firestore不是函数-无法解析模块说明符"firebase". [英] Firestore onSnapshot - firebase.firestore is not a function - Failed to resolve module specifier "firebase"

查看:155
本文介绍了Firestore onSnapshot-firebase.firestore不是函数-无法解析模块说明符"firebase".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想学习Firebase,我想使用onSnapshot,因此从实时数据库"切换为"firestore".切换后,我复制了以下样板代码:

I'm just trying to learn firebase, and I wanted to use onSnapshot so switched from "realtime database" to "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}`);
    });

});

但是我得到了"firebase.firestore不是函数".我在这里找到了解决方案: https://stackoverflow.com/a/50684682/4907950 只是添加了:

However I got that "firebase.firestore is not a function". I found the solution here: https://stackoverflow.com/a/50684682/4907950 which is just adding:

import * as firebase from 'firebase';
import 'firebase/firestore';

但是,然后我得到:"Uncaught TypeError:无法解析模块说明符" firebase.相对引用必须以"/," ./或" ../开头."

However, then I get: "Uncaught TypeError: Failed to resolve module specifier "firebase". Relative references must start with either "/", "./", or "../"."

还值得注意的是,我已将< script defer src ="/__/firebase/7.14.2/firebase-firestore.js"></script> 添加到我的HTML文件,错误仍然存​​在.

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.

推荐答案

出于某种原因,我不得不使用完整的URL,并将其保留在< BODY> 标记的顶部,如下所示:

For some reason I had to use the complete URL and keep them at top of <BODY> tag like this:

<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>

我还必须使用Firebase CLI重新安装所有需要的东西.此后效果很好.

Also I had to re-install all requires stuff using Firebase CLI. It worked perfectly thereafter.

这篇关于Firestore onSnapshot-firebase.firestore不是函数-无法解析模块说明符"firebase".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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