如何在 Firebase 实时数据库(web)中插入生成的 GUID 值?

How to insert the generated GUID values in Firebase Realtime Database(web)?

我已经生成了 GUID 值,但是当我点击“提交”按钮时,这些值没有存储在 Firebase Realtime 中 Database.I 无法找到 error.Null 值存储在数据库。

这是代码。

<html>
    <head>
    <script src="https://www.gstatic.com/firebasejs/4.8.1/firebase.js"></script>
<script>
    function license() {
    return Math.floor((1 + Math.random()) * 0x100000)
      .toString(16)
      .substring(1);
  }
         var val=license();
    document.write(val);

var myFirebase = firebase.database().ref();

    function fun(){
        var id = document.getElementById("number").value;

  var config = {
    apiKey: "AIzaSyAw9IGgteG1Zis_oZ7t0V6xwgfJsiBMVZE",
    authDomain: "product-license.firebaseapp.com",
    databaseURL: "https://product-license.firebaseio.com",
    projectId: "product-license",
    storageBucket: "product-license.appspot.com",
    messagingSenderId: "54021411813"
  };
  firebase.initializeApp(config);

var recommendations = myFirebase.child("recommendations");


recommendations.push({
    "Id": id,
    "value": val,

});   
    }
</script>
    </head>
<body>
<form align="center">
        <input id="number" placeholder="Enter the Number"/>
    <br>
    <br>
        <input type="submit" value="SUBMIT" onclick="fun()"/>
    </form>
</body>
</html>

您应该首先初始化 firebase,然后再调用 firebase.database().ref()。此外,在推送地图值时不需要双引号。

<html>
<head>
<script src="https://www.gstatic.com/firebasejs/4.8.1/firebase.js"></script>
<script>
    var config = {
    apiKey: "AIzaSyAw9IGgteG1Zis_oZ7t0V6xwgfJsiBMVZE",
    authDomain: "product-license.firebaseapp.com",
    databaseURL: "https://product-license.firebaseio.com",
    projectId: "product-license",
    storageBucket: "product-license.appspot.com",
    messagingSenderId: "54021411813"
  };
  firebase.initializeApp(config);

    function license() {
    return Math.floor((1 + Math.random()) * 0x100000)
      .toString(16)
      .substring(1);
  }
    var val=license();
    document.write(val);

    var myFirebase = firebase.database().ref();

    function fun(){
        var id = document.getElementById("number").value;

        var recommendations = myFirebase.child("recommendations");

        recommendations.push({
            Id: id,
            value: val,
        });   
}
</script>
</head>
<body>
<form align="center">
        <input id="number" placeholder="Enter the Number"/>
    <br>
    <br>
        <input type="submit" value="SUBMIT" onclick="fun()"/>
    </form>
</body>
</html>