【问题标题】:Update values to firebase将值更新到 firebase
【发布时间】:2019-10-18 17:24:03
【问题描述】:

Firebase 实时数据库中的值未更新。我是 Web 开发的新手,但我知道一点 html。你能检查一下这里有什么问题吗? 对于这个问题,我故意将 Firebase Config 留空。 我尝试使用 Firebase 文档中给出的大部分代码。

<!DOCTYPE html>
<html>


<head>
<title>"Web App"</title>
</head>
<body>

<div class="mainDiv" align="left">
  <h1 align="left">"Firebase web page"</h1>
  <textarea id="Command" placeholder="ON/OFF" stClyle="text align:left; overflow:auto; border:6px outset #000000;"></textarea>
  <button id="Submit" onclick="submitclick()">Click Me!</button>
</div>

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.1.0/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
 https://firebase.google.com/docs/web/setup#config-web-app -->

<script>
// Your web app's Firebase configuration
var firebaseConfig = {

apiKey: "___________________________",
authDomain: "_________.firebaseapp.com",
databaseURL: "https://___________.firebaseio.com",
projectId: "________",
storageBucket: "_________.appspot.com",
messagingSenderId: "________",
appId: "_____________"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

var mainTxt= document.getElementById("Command"); 
var submitbtn = document.getElementById("Submit");
function submitclick()
{

var com = mainTxt.value;
firebase.database().ref().child("username").set(com);
}
</script>

</body>

</html>

【问题讨论】:

  • firebase.dtabase().ref().child(...).set(..) 的数据库中有错字。应该是firebase.database().ref().child(....).set(...)
  • 是的.. 抱歉。我添加了我在此处复制代码的那一行。但这并不能解决问题,它仍然无法正常工作。
  • 控制台有错误吗?
  • 我主要有 1 个孩子:“用户名”...我也已经设置了网络应用程序。
  • 在规则中读写都是真的

标签: html firebase firebase-realtime-database


【解决方案1】:

首先要使用firebase,你必须包含

<script src="https://www.gstatic.com/firebasejs/6.1.0/firebase-firestore.js"></script>

仍然无法正常工作,因为这不是您写入 firebase 的方式。你写成键值对。试试

firebase.database().collection("userNames").add({name : "user_name"});

或者如果您尝试更新文档

firebase.database().ref().child("userName").set({name : "user_name"});

这里是官方文档:Add and Manage Data

就主题而言,调用 .set().add() 可能并不总是写入必须使用 then() 检查的数据strong> 和 catch()

因此,在 firebase 中更新文档值的完整示例如下:

// Add a new document in collection "cities"
db.collection("users").doc("userName").set({
    name: "USER_NAME"
})
.then(function() {
    console.log("Document successfully written!");
})
.catch(function(error) {
    console.error("Error writing document: ", error);
}); 

【讨论】:

    【解决方案2】:

    查看有关如何将 Firebase 添加到您的 Web/JavaScript 项目的文档:https://firebase.google.com/docs/web/setup

    通过做

    &lt;script src="https://www.gstatic.com/firebasejs/6.1.0/firebase-app.js"&gt;&lt;/script&gt;

    您正在添加 Firebase core 库,但 这还不够。您必须为要使用的服务添加库,在您的情况下是实时数据库。

    所以你需要这样做

    <script src="https://www.gstatic.com/firebasejs/6.1.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/6.1.0/firebase-database.js"></script>
    

    注意下面一行

    <!-- TODO: Add SDKs for Firebase products that you want to use
    

    在您自己的代码中:它准确地表明了上面描述的内容。

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多