您正在处理异步数据流,因此您应该监听submitPost函数之外的数据.
var myDataRef = new Firebase('https://EXAMPLE.firebaseio.com/'); myDataRef.on('child_added', function(snapshot) { var post = snapshot.val(); displayUserPost(post.name, post.text); }); function submitPost(e) { var myDataRef = new Firebase('https://EXAMPLE.firebaseio.com/'); var name = $('#titleInput').val(); var text = $('#postInput').val(); myDataRef.push({name: name, text: text}); $('#postInput').val(''); e.preventDefault(); } function displayUserPost(name, text) { $('').text(text).prepend($('').text(name+': ')).appendTo($('#PostsDiv')); $('#PostsDiv')[0].scrollTop = $('#PostsDiv')[0].scrollHeight; }