Javascript Firebase:Firebase应用程序名为';[默认]';在JS中

Javascript Firebase:Firebase应用程序名为';[默认]';在JS中,javascript,firebase,web,Javascript,Firebase,Web,我正在尝试将我的项目与Cloud Firestore连接,但当我输入必要的代码时,出现以下错误: errors.ts:91 Uncaught FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app). at initializeApp (https://www.gstatic.com/firebasejs/8.6.2/firebase-app.js:1:17950) at

我正在尝试将我的项目与Cloud Firestore连接,但当我输入必要的代码时,出现以下错误:

errors.ts:91 Uncaught FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).
at initializeApp (https://www.gstatic.com/firebasejs/8.6.2/firebase-app.js:1:17950)
at Object.H.initializeApp (https://www.gstatic.com/firebasejs/8.6.2/firebase-app.js:1:21516)
at file:///C:/Users/Angel/Desktop/Imagine/index.js:28:10
我写的代码是:

firebase.initializeApp({
  apiKey: "myapikey",
  authDomain: "mydomain",
  projectId: "myprojectid"
});

var db = firebase.firestore();

db.collection("users").add({
  first: "Ada",
  last: "Lovelace",
  born: 1815
})
.then((docRef) => {
  console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
  console.error("Error adding document: ", error);
});

我尝试了这个方法,但不起作用:如果(!firebase.apps.length){firebase.initializeApp({});}否则{firebase.app();//如果已经初始化,请使用该方法}