如何在Firestore数据库中手动将文档添加到集合? [英] How to manually add a document to a collection in Firestore database?

查看:88
本文介绍了如何在Firestore数据库中手动将文档添加到集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a quite simple database in Firestore (geographic name, lat and Lon). The database is very static and I only need to add or remove a record (document) once a while. How can I add a record manually to the collection, with the same fields as the other documents? When I press "Add document" the console asks me to input each field (see screenshot).

推荐答案

下面的HTML页面将允许您写入您的 spots Firestore集合.

The following HTML page will allow you to write to your spots Firestore collection.

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

You need to adapt the fields, of course, as well as the Firebase config.

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

If you want to authenticate, just add two extra fields e.g. Username and Password and use the signInWithEmailAndPassword() method. (I can adapt the page if you want).

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

You can host this page in Firebase hosting for example, taking advantage of the SSL Certificate. Or you can simply save it on your computer and open it with a browser (not HTTPS in this case, but a good way to test).

<!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>

这篇关于如何在Firestore数据库中手动将文档添加到集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆