【问题标题】:how to show/display the acquired data from database using snapshot/querysnapshot in my designed html page?如何在我设计的 html 页面中使用快照/查询快照显示/显示从数据库获取的数据?
【发布时间】:2020-04-26 19:09:05
【问题描述】:

我尝试了.innerhtml 的方式,但不知道为什么它只显示一个空白页。

当我使用snapshot 而不是querysnapshot 时,它可以工作,但显示不同(最后输入的文档)而不是搜索的。

功能代码

function renderHTML(doc){
  // TODO: do something with the data 

  // To create elements (such as list or span) 
  var plano = document.createElement('span');
  plano.innerHTML = doc.data().plate;
  // list.appendChild(plano);

  // To append the the list to querySelector
  document.getElementsByClassName(".numPlate").appendChild(plano);
 }
function ser(){
  n=0;
  var num = document.getElementById("plateNo").value;
  if(num == ""){
    var ent = document.getElementById("plateNo");
    ent.classList.add("invalid");
    document.getElementById("err").setAttribute("data-error", "Wrong");
  }
  else{
    for(i=0;i<2;i++){
        var ans = num.localeCompare(plates[i]); 
        if(ans==0){
          db.collection("details").where('plate', '==', num).get()
            .then(snapshot => {
               if (snapshot.empty) {
                 console.log('No matching documents.');
               return;
                }  
            snapshot.forEach(doc => {
                console.log(doc.id, '=>', doc.data());
          renderHTML(doc);
          });
        })
           .catch(err => {
           console.log('Error getting documents', err);
        });
           n=1;
           window.open("/pages/detail.html","_self");
           break;
        }}
        if(n==0) {
          var element = document.getElementById("plateNo");
          element.classList.add("invalid");
          document.getElementById("err").setAttribute("data-error", "Invalid Entry");
        }
  }}

输入页面的 HTML 代码

<form id="platef" action="/" method="get" target="_self" name="serf" class="col s12 l12">
            <div class="row">
              <div class="input-field col s7 l3 offset-s2 offset-l4 asd">
                <i class="material-icons prefix" id="ic" style="top: 12px;" onclick="ser()" required>search</i>
                <input id="plateNo" class="validate" type="text" name="no" style="text-transform:uppercase" onsubmit="ser()" required>
                <label for="plateNo">Number plate</label>
                <span class="helper-text" data-error="Invalid Entry" id="err"></span>
              </div>
            </div>
          </form>

显示页面的 HTML 代码

<div id="details" class="details container">
<div class="row">
      <div class="col s6 offset-s3 l6 offset-l3">
    <div class="head card-panel">
      <div class="header center-align"><h5 class="numPlate">Database Data</h5></div>
    </div></div></div></div></div>
</div>

更新

我正在从数据库中获取数据并将其显示在 console.log 中,但未显示在网页上。对于设计,我使用 Materializecss。

我有两页,我输入一个数字在数据库中搜索,然后在另一页上显示它(如果有效)。

【问题讨论】:

    标签: javascript html firebase google-cloud-firestore


    【解决方案1】:

    以 HTML 格式呈现一些 Cloud Firestore 数据:

    // To select the element class details
    const dat = document.querySelector(".details");
    
    
    function renderHTML(doc){
     // TODO: do something with the data 
    
     // To create elements (such as list or span) 
     let list = document.createElement('li');
     let name = document.createElement('span');
    
     // To set attributes to elements 
     list.setAttribute('id', doc.id);
     name.textContent = doc.data().name;
    
     // To append element to the list 
     list.appendChild(name);
    
     // To append the the list to querySelector
     dat.appendChild(list);
    }
    
    
    db.collection("details").where('plate', '==', num).get()
      .then(snapshot => {
        if (snapshot.empty) {
          console.log('No matching documents.');
          return;
        }  
        snapshot.forEach(doc => {
          console.log(doc.id, '=>', doc.data());
          renderHTML(doc);
        });
      })
      .catch(err => {
        console.log('Error getting documents', err);
      });
    

    更多信息请访问documentation

    【讨论】:

    • 谢谢!!这帮助我获得了特定的文档。你能帮我解决这个函数中数据的 .innerhtml 表示吗?问题是它没有显示仅加载空白但特定文档大小的数据。我尝试创建元素,但似乎不起作用。 感谢您的帮助
    • 类似于.textContent,使用纯文本String,不解析成HTML,可以使用.innerHTML插入文本,如var dat = document.createElement("P"); dat.innerHTML = "This is a paragraph.";。更多 HTML 信息可以在here找到。
    • 先生! 感谢您的帮助。我仍然没有得到输出,请重新检查问题,因为我也更新和编辑了代码。
    • 很高兴听到您正确查询数据。您是否按预期接收来自表单的输入?仅在显示数据时有问题吗?如果是这样,您可能想尝试plano.textContent = doc.data().plate 并确保将其附加到正确的元素。请注意,这是一个不同的问题,可能需要在论坛中提出新问题。
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多