FIREBASE警告:用户回调引发了异常。 TypeError:无法读取null的属性“text”

时间:2017-04-25 15:40:03

标签: javascript html firebase firebase-realtime-database

我一直使用firebox制作实时文本框。工作流程是这样的,给出了带文本框的URL,如果有人实时打开相同的URL,他/她应该看到确切的文本框内容。我收到了警告:

<html>
<script src="https://www.gstatic.com/firebasejs/3.8.0/firebase.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

<script>

  var config = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: ""
  };

  firebase.initializeApp(config);


    var database = firebase.database().ref(); //get a reference to firebase database service
var textboxRef = database.child('textbox'); //Get a reference to the "textbox" child
var $textbox = $('#textbox'); //Saves a reference to the textarea element using jQuery

//When anything changes (added, removed, updated) in the "textbox" child in the database
textboxRef.on('value', function(snapshot) {
  console.log("From Firebase:", snapshot.val());
  $textbox.val(snapshot.val().text); //Set the value of the textarea to the value returned by Firebase (specifically the "text" property of the "textbox" child)
});

//When any key is released while the textarea is in focus
$textbox.keyup(function() {
  //Updates the properties and values in the database child called "textbox" with the object provided. The "set" function is used instead of the "update" function so that if the database child "textbox" of its properties are removed, this will create it as well as update it
  textboxRef.set({
    text: $textbox.val()
  });
});
</script>

<link rel="stylesheet" href="style.css">
<textarea name="textbox" id="textbox" placeholder="Type here"></textarea>

</html>

我对Firebase实时数据库也很陌生。

0 个答案:

没有答案