简体   繁体   中英

Firebase how to Order by child on js

I can't order a message with timestamp, I tried many ways and it still doesn't work, please help An arrangement with data like pictures

code database

data:

  { chatRoom : {
      "232": {
        "-NKHmBkCihoyRiYABX5O": {
          "createdAtDate": "2022-12-27",
          "createdAtTime": "17:52:49",
          "fromType": 4,
          "message": "message2",
          "readByAdmin": false,
          "readByUser": true,
          "timestamp": 1672131169.174558,
          "user": "tuan do",
          "userId": "232"
        }
      },
      "242": {
        "-NKHm6svnBJhWq321aNv": {
          "createdAtDate": "2022-12-27",
          "createdAtTime": "17:52:29",
          "fromType": 3,
          "message": "message1",
          "readByAdmin": false,
          "readByUser": true,
          "timestamp": 1672131149.252743,
          "user": "solashi 歯科衛生士",
          "userId": "242"
        }
      }
    }
}

code: const dbRefAdmin = firebase.database().ref('chatRoom').orderByChild('timestamp').limitToLast(10)

To arrange data like pictures in Firebase, you can use the orderByChild() method to specify the child key that you want to use for ordering the data. You can then use the startAt(), endAt(), and equalTo() methods to filter the results of the query based on the value of the child.

Here's an example of how to use orderByChild() to arrange data like pictures:

// Get a reference to the database
var database = firebase.database();

// Get a reference to the 'pictures' child of the database
var picturesRef = database.ref("pictures");

// Create a query to order the pictures by their title
var query = picturesRef.orderByChild("title");

// Execute the query and process the results
query.once("value").then(function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var key = childSnapshot.key;
    var childData = childSnapshot.val();
    console.log("key: " + key + ", title: " + childData.title);
  });
});

In this example, the query object orders the pictures in the pictures child of the database by their title. The once("value") method is used to execute the query and get the results as a snapshot. The snapshot contains a list of the children that match the query, and the forEach() method is used to iterate over the list and process the results.

You can also use the startAt(), endAt(), and equalTo() methods to filter the results of the query based on the value of the title child. For example, you can use the following code to get all pictures whose title starts with the letter "A":

var query = picturesRef.orderByChild("title").startAt("A");

Or you can use the following code to get all pictures whose title is "Sunset":

var query = picturesRef.orderByChild("title").equalTo("Sunset");

I hope this helps. Let me know if you have any questions.

A simple answer:

  • Try to use " orderBy " instead of "orderByChild "
  • then " limit " instead of " limitToLast "

Give a feedback in comment and put your post in solved status if the code below solves your problem.

Official documentation: order and limit data with Cloud Firestore

 var is_start_load = true; const dbRefAdmin = firebase.database().ref('chatRoom').orderBy('timestamp').limit(10);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM