如何手动将文档添加到 Firestore 数据库中的集合?

How to manually add a document to a collection in Firestore database?

我在 Firestore 中有一个非常简单的数据库(地理名称、纬度和经度)。数据库是非常静态的,我只需要偶尔添加或删除一条记录(文档)。 如何手动将记录添加到集合中,并使用与其他文档相同的字段?当我按 "Add document" 时,控制台要求我输入每个字段(见屏幕截图)。

你不能,你必须编写或使用工具来做到这一点。

以下 HTML 页面将允许您写入 spots Firestore 集合。

当然,您需要调整字段以及 Firebase 配置。

如果要进行身份验证,只需添加两个额外的字段,例如用户名和密码并使用 signInWithEmailAndPassword() 方法。 (如果你愿意,我可以调整页面)。

例如,您可以在 Firebase 托管中托管此页面,利用 SSL 证书。或者您可以简单地将它保存在您的计算机上并使用浏览器打开它(在这种情况下不是 HTTPS,但测试的好方法)。

<!DOCTYPE html>
<html>
  <head>
    <title>Firebase Form</title>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
    <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
    <script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-app.js"></script>

    <!-- Add Firebase products that you want to use -->
    <script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js"></script>
    <script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-auth.js"></script>
  </head>

  <body>
    <div>
      <p>Name:</p>

      <input type="text" placeholder="Name" id="name" />

      <p>City:</p>

      <input type="text" placeholder="City" id="city" />

      <br /><br />
      <input type="submit" value="submit" class="submit" id="submit" />
    </div>

    <script>
      $(document).ready(function() {
        // Initialize Firebase
        var config = {
          apiKey: 'xxxxxxxxxxxxx',
          authDomain: 'xxxxxxxxxxxxx',
          databaseURL: 'xxxxxxxxxxxxx',
          projectId: 'xxxxxxxxxxxxx'
        };

        firebase.initializeApp(config);

        var database = firebase.firestore();

        $('#submit').on('click', function() {
          var nameValue = $('#name').val();
          var cityValue = $('#city').val();

          var dataObject = {
            name: nameValue,
            city: cityValue
          };

          database.collection('spots').add(dataObject);
        });
      });
    </script>
  </body>
</html>