为什么我的程序中断了?为什么会出现此错误?

时间:2019-07-12 21:05:54

标签: javascript reactjs firebase

我正在尝试使用React和Firebase编写程序。我遇到错误:

Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).

17 | firebase.initializeApp(firebaseConfig); | 
^ 
18 | function verify(){ 
19 | var server = firebase.database().ref('servers/' + document.getElementById("code").value); 
20 | server.on('value', function(snapshot) {

我为什么要得到它,怎么解决?

我的代码:

import React from 'react';
import * as firebase from 'firebase';
import "firebase/app";
import "firebase/database";

function Login() {
  var firebaseConfig = {
    apiKey: "-",
    authDomain: "-",
    databaseURL: "-",
    projectId: "-",
    storageBucket: "-",
    messagingSenderId: "-",
    appId: "-"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  function verify(){
    var server = firebase.database().ref('servers/' + document.getElementById("code").value);
    server.on('value', function(snapshot) {

      if(snapshot.val() && snapshot.val().exist)
      {
        console.log(snapshot.val()); 
        alert("sucsess");
      } else{
        alert("fail");
      }
    });
  }
  function checkEnter(event) {
    if(event.key === 'Enter') {
      verify(); 
    }
  }


  return (
    <div>
      <div className="title"><span className="title-span">//</span> ChatRooms <span className="title-span">//</span></div>
       <div className="Login">
         <input className="code" type="text" id="code" placeholder="Your chat code..." onKeyDown={checkEnter}/>
         <button onClick={verify} className="verify">Verify</button>
       </div>
    </div>
  );
}

export default Login;

1 个答案:

答案 0 :(得分:1)

请将Left Date替换为:

firebase.initializeApp(firebaseConfig);

解释与我在评论中告诉您的相同,我希望就足够了。