Firestore(网络)添加简单集合

时间:2019-04-15 12:34:44

标签: javascript firebase google-cloud-firestore

我尝试将简单的测试集合添加到我的Firestore数据库中。我的托管也在firebase上。我已经在html的头中生成了初始化数据:

<script defer src="/__/firebase/5.9.3/firebase-auth.js"></script>
<script defer src="/__/firebase/5.9.3/firebase-database.js"></script>
<script defer src="/__/firebase/5.9.3/firebase-firestore.js"></script>
<script defer src="/__/firebase/5.9.3/firebase-messaging.js"></script>
<script defer src="/__/firebase/5.9.3/firebase-storage.js"></script>
<!-- initialize the SDK after all desired features are loaded -->
<script defer src="/__/firebase/init.js"></script>

init.js在哪里

if (typeof firebase === 'undefined') throw new Error('hosting/init-error: Firebase SDK not detected. You must include it before /__/firebase/init.js');
firebase.initializeApp({
    "apiKey": "...",
    "databaseURL": "https://intorry3.firebaseio.com",
    "storageBucket": "intorry3.appspot.com",
    "authDomain": "intorry3.firebaseapp.com",
    "messagingSenderId": "1...",
    "projectId": "intorry3"
});

和我的代码(在HTML正文的末尾)

function waretest() {
  let db = firebase.firestore();

  // Add a new document in collection "cities"
  db.collection("cities").doc("LA").set({
      name: "Los Angeles",
      state: "CA",
      country: "USA"
  })
  .then(function() {
      console.log("Document successfully written!");
      document.getElementById('status').textContent = "collection created";
  })
  .catch(function(error) {
      console.error("Error writing document: ", error);
      document.getElementById('status').textContent = error;
  });
}

function initApp() {
  firebase.auth().onAuthStateChanged(function(user) {
    ...               
  });

  document.getElementById('warehousesBTN').addEventListener('click', waretest, false);
}

window.onload = function() {
  initApp();
};

它不起作用。问题出在哪里?我应该再声明些什么吗?

1 个答案:

答案 0 :(得分:1)

您正在混合通过from reserved URLs加载Firebase SDK Firebase config object.和配置的选项

尝试以下HTML页面(通过Firebase配置对象),它应该可以工作:

<html>

<head>

<!-- Firebase App is always required and must be first -->
<script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-app.js"></script>

<!-- Add additional services that you want to use -->
<script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-messaging.js"></script>


</head>

<body>

 <button type="button" id="warehousesBTN">Add data to Firestore</button> 
 <div id="status"></div>
 <script>

  var config = {
    "apiKey": "...",
    "databaseURL": "https://intorry3.firebaseio.com",
    "storageBucket": "intorry3.appspot.com",
    "authDomain": "intorry3.firebaseapp.com",
    "projectId": "intorry3"
  };
  firebase.initializeApp(config);


    function waretest() {
      let db = firebase.firestore();
      // Add a new document in collection "cities"
      db.collection("cities").doc("LA").set({
          name: "Los Angeles",
          state: "CA",
          country: "USA"
      })
      .then(function() {
          console.log("Document successfully written!");
          document.getElementById('status').textContent = "document created";
      })
      .catch(function(error) {
          console.error("Error writing document: ", error);
          document.getElementById('status').textContent = error;
      });
      }

     document.getElementById('warehousesBTN').addEventListener('click', waretest);

</script>
</body>
</html>