[英]Server Error TypeError: firebaseApp.firestore is not a function error
Here is my code:这是我的代码:
import * as firebase from "firebase/app";
<script src="https://www.gstatic.com/firebasejs/8.7.0/firebase-app.js"></script>
//import "firebase/firestore";
//import {firebase} from 'firebase/app';
import 'firebase/firestore';
const firebaseConfig = {
apiKey: "A*******************",
authDomain: "u*****.com",
projectId: "u******a",
storageBucket: "unl***********.com",
messagingSenderId: "5***********",
appId: "1:5************",
measurementId: "G-"
};
// Initialize Firebase
const firebaseApp =firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
export { db };
Troubleshooting done so far:目前已解决的问题:
1.Tried importing "import * as firebase from "firebase/app" and import 'firebase/firestore'; 1.尝试从“firebase/app”导入“import * as firebase”并导入“firebase/firestore”;
re-installed npm.重新安装 npm。
deleted nodes and package-lock.json and reinstalled them.删除节点和 package-lock.json 并重新安装它们。
Installed firebase and firestore.安装了 firebase 和 firestore。
You are using Firebase SDK version 9 , the way you import and initialize your Firebase SDK is for older version that's why you are receiving that error.您正在使用Firebase SDK 版本 9 ,导入和初始化 Firebase SDK 的方式适用于旧版本,这就是您收到该错误的原因。
Use Firebase version 8 or below to still run your current code without any changes.使用Firebase 版本 8 或更低版本仍然可以运行您当前的代码而无需任何更改。 You can change it in
package.json
or by running the command below:您可以在
package.json
或运行以下命令来更改它:
npm install firebase@8.0.0 --save
the sample dependencies in package.json
: package.json
中的示例依赖项:
"dependencies": {
"firebase": "8.0.0",
"next": "11.1.2",
"react": "17.0.2",
"react-dom": "17.0.2"
}
But if you want to use the latest Firebase version 9.0.0
and above which is implementing modular API and you want to adapt the modular API. Use compat
packages, in your case, it should be:但是,如果您想使用最新的Firebase 版本
9.0.0
及更高版本,它正在实现模块化 API ,并且您想要调整模块化 API。使用compat
包,在您的情况下,它应该是:
import * as firebase from "firebase/compat/app";
import 'firebase/compat/firestore';
the
compat
libraries are a temporary solution that will be removed completely in a future major SDK version ( such as version 10 or version 11 ).compat
库是一个临时解决方案,将在未来的主要 SDK 版本(例如版本 10 或版本 11 )中完全删除。 Your ultimate goal is to remove compat code and keep only version 9 modular-style code in your app.您的最终目标是删除兼容代码并在您的应用程序中仅保留第 9 版模块化样式代码。
Like Dharmaraj's answer , you should upgrade from version 8 to the modular Web SDK.就像Dharmaraj 的回答一样,您应该从版本 8 升级到模块化 Web SDK。
However I'd recommend upgrading to Modular SDK syntax because has supports tree-shaking and will be lighter than existing version.
但是我建议升级到 Modular SDK 语法,因为它支持 tree-shaking 并且比现有版本更轻。 Checkout this Firecast to learn more about new syntax for Firestore: Getting started with Cloud Firestore for Web
查看此 Firecast 以了解有关 Firestore 新语法的更多信息: Getting started with Cloud Firestore for Web
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.