數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)


問題描述

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

我正在嘗試將數據發佈到 firebase,但沒有發布。我嘗試使用 addEventListener 發布要發布的數據,但無濟於事。這是我的 html,帶有包含 firebase sdk 的外部 main.js:

<BODY>
<TABLE align="center">
<caption></caption>
<tr>
<td width=500 height=150 colspan=8 style="text‑align: center"><strong
style="font‑size:50">Add an Entry</strong></td>
</tr>
<tr>
<TD height=500 colspan=8>

        &lt;form id=&quot;info&quot;&gt;
            &lt;table align=&quot;center&quot;&gt;

                &lt;tr&gt;
                    &lt;td&gt;Name:&lt;/td&gt;
                    &lt;th&gt;&lt;INPUT type=&quot;text&quot; Size=&quot;40&quot; Maxlength=&quot;35&quot;
                        Name=&quot;personal‑name&quot; id=&quot;name&quot; placeholder=&quot;Your Name Here&quot;&gt;
                    &lt;/th&gt;

                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Email:&lt;/td&gt;
                    &lt;th&gt;&lt;INPUT type=&quot;text&quot; Size=&quot;40&quot; Maxlength=&quot;40&quot;
                        Name=&quot;personal‑email&quot; id=&quot;email&quot; placeholder=&quot;Your Email Here&quot;&gt;
                    &lt;/th&gt;

                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Address:&lt;/td&gt;
                    &lt;th&gt;&lt;textarea rows=&quot;5&quot; width=&quot;550&quot; type=&quot;text&quot; Size=&quot;40&quot;
                            Maxlength=&quot;40&quot; Name=&quot;address&quot; id=&quot;address&quot;
                            placeholder=&quot;__&quot;&gt;&lt;/textarea&gt;
                    &lt;/th&gt;

                &lt;/tr&gt;
            &lt;/table&gt;
            &lt;br&gt;
            &lt;div align=&quot;center&quot;&gt;
                &lt;button id=&quot;refresh&quot;&gt;refresh&lt;/button&gt;
                &lt;button id=&quot;addBtn&quot; &gt;submit&lt;/button&gt;
            &lt;/div&gt;

        &lt;/form&gt;
    &lt;/td&gt;
&lt;/tr&gt;

</Table>
</Body>
<script src="https://www.gstatic.com/firebasejs/7.16.1/firebase‑app.js&quot;&gt;&lt;/script&gt;
<script src="main.js"></script> </BODY>
</code></pre>

這是我的 main.js:

// Firebase 配置,然後是 eventlistener

var firebaseConfig = {
apiKey: **,
authDomain: **,
databaseURL: **,
projectId: **,
storageBucket: **,
messagingSenderId: **,
appId: **,
measurementId: **,
};

firebase.initializeApp(firebaseConfig);
firebase.analytics();

form.addEventListener("submit", (e)=>{
e.preventDefault();
firebase.firestore().collection("information").add({
name:document.getElementById('name').value,
email:document.getElementById('email').value,
address:document.getElementById('address').value
})
});
</code></pre>


參考解法

方法 1:

4 things:

Make sure you define a form variable using the form's id

const form = document.getElementById("info");

You are missing the analytics and firestore sdk src (I included auth incase you plan to use it)

<!‑‑ Firebase App (the core Firebase SDK) is always required and must be listed first ‑‑>
<script src="https://www.gstatic.com/firebasejs/7.16.1/firebase‑app.js"></script>
<!‑‑ If you enabled Analytics in your project, add the Firebase SDK for Analytics ‑‑>
<script src="https://www.gstatic.com/firebasejs/7.16.1/firebase‑analytics.js"></script>
<!‑‑ Add Firebase products that you want to use ‑‑>
<script src="https://www.gstatic.com/firebasejs/7.16.1/firebase‑auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.16.1/firebase‑firestore.js"></script>

In your firebase console, change your database rules' conditional to true (if not already)

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

For performance, you should store your product initializations and collections in variables

const analytics = firebase.analytics();

const firestore = firebase.firestore(); 
const information = firestore.collection("information"); 

const form = document.getElementById("info");
form.addEventListener("submit", (e)=>{
  e.preventDefault();
  information.add({
    name:document.getElementById('name').value,
    email:document.getElementById('email').value,
    address:document.getElementById('address').value
  })
});

Alternatively, you can create a doc with a name for faster querying

information.doc(document.getElementById('name').value).set({
    name:document.getElementById('name').value,
    email:document.getElementById('email').value,
    address:document.getElementById('address').value
 })

(by W JongPavlos Karalis)

參考文件

  1. Data not posting.. Help! html,js,firebase (CC BY‑SA 2.5/3.0/4.0)

#javascript #html






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論