Jquery + Firebase:如何将日期连接到状态帖子?

时间:2016-10-28 00:14:08

标签: jquery html firebase firebase-realtime-database

我正在为大学创建一个网站,其中包含人们添加的状态更新。发布后,单词或句子将发送到我的Firebase实时数据库帐户,然后存储该帐户。

我想在jquery中添加一些东西,但是我对jquery并不擅长 - 我不太了解它。

是否有办法添加日期,以便每次发布帖子时,日期都会添加到底部 - 表示添加日期。

这是我的firebase jquery代码:

<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyBcVJsd1tQqEFKeWZ5VD1AMh79BfDnNlyk",
authDomain: "project-3-7caba.firebaseapp.com",
databaseURL: "https://project-3-7caba.firebaseio.com",
storageBucket: "project-3-7caba.appspot.com",
messagingSenderId: "595847032866"
};
firebase.initializeApp(config);

var database = firebase.database();

function saveData(){
var userInputText = $('#userText').val();

database.ref('/').push({
hello: userInputText,
color: '#333333'
});
}


database.ref('/').on('value', function(snapshot) {
$('#timeline').empty();

var data = snapshot.val();
console.log(data);

$.each(data, function( index, value ) {
console.log(value.hello);
$( "#timeline" ).prepend( "<p>" + value.hello + "</p>" );
$("#userText").val('');
});

});
</script>

我一直在看Matt Bradley的Livestamp

1 个答案:

答案 0 :(得分:0)

写作:

为您的数据添加date,如下所示:

database.ref('/').push({
hello: userInputText,
color: '#333333',
date: new Date()
});

读数:

  database.ref('/').on('value', function(snapshot) {
  var data = snapshot.val();
  $.each(data, function( index, value ) {

    // do something with: value.date

  });

  });