0

Hi i am making a chatting website and i wanna reverse the order of messages recieved this is how i send the messages into database `

function send() {
  msg = document.getElementById("msg").value;
  firebase.database().ref(room_name).push({
    name: user_name,
    message: msg,
    date: "date: " + datedate1 + " - " + datemonth1 + " - " + dateyear1,
    time: "Time : " + datehour1 + " : " + datemin1 + " . " + datesec1,
    timestamp: firebase.database.ServerValue.TIMESTAMP,
  });

  document.getElementById("msg").value = "";
}

this is how i get the data from firebase

function getData() {
  firebase.database().ref("/" + room_name).on('value', function (snapshot) {
    document.getElementById("output").innerHTML = "";
    snapshot.forEach(function (childSnapshot) {
      childKey = childSnapshot.key;
      childData = childSnapshot.val();
      if (childKey != "purpose") {
        firebase_message_id = childKey;
        message_data = childData;
        //Start code
        console.log(firebase_message_id);
        console.log(message_data);
        name = message_data['name'];
        message = message_data['message'];
        like = message_data['like'];
        date = message_data['date'];
        time = message_data['time'];
        name_with_tag = "<h4> " + name + "<img class='user_tick sub-text' src='tick.png'></h4>";
        message_with_tag = "<h4 class='message_h4 sub-text'>" + message + "</h4>";
        span_with_tag = "<span class='glyphicon glyphicon-thumbs-up'> Like: " + like + "</span></button><hr>";
        space = "<h5><span>•~~~ ~~~ ~~~ ~~~ ~~~•</span></h5>";


        time = "<h6 style='text-align: right; float: right;'>" + time + "</h6>";
        row = name_with_tag + message_with_tag + space;
        document.getElementById("output").innerHTML += time += row;
        //End code
      }
    });
  });
}

i was trying to get it by timestamp aka the newest first and old ones later ut was unable to do it

0 Answers0