admin管理员组文章数量:1410730
I'm writing in the realtime database using the push()
function
The push()
function orders the input data in chronological order, but it does not, and it orders the data in numerical order
I want the data to be entered in chronological order The userName is not based on timestamp, but the data is recorded in the order of 0107 after 0104
"0104": {
"-OKQu5uz3y6aq_exRrwA": {
"contents": "1",
"timestamp": "2025-03-03T12:08:34.229Z",
"userName": "0104"
}
},
"0107": {
"-OHJRj4ye8DOxw0UbEo1": {
"contents": "1",
"timestamp": "2025-01-23T18:35:48.476Z",
"userName": "0107"
}
},
"0107": {
"-OJRQ1IGgYYgmk-ri7Ye": {
"contents": "1 ",
"timestamp": "2025-02-19T04:16:44.235Z",
"userName": "0107"
},
<script type="module">
import { initializeApp } from ".1.0/firebase-app.js";
import { getDatabase, ref, set, get, child, push } from ".1.0/firebase-database.js";
import { getAnalytics } from ".1.0/firebase-analytics.js";
const firebaseConfig = {My Firebase Info};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getDatabase(app);
document.getElementById("submit").addEventListener('click', function (e) {
e.preventDefault();
const currentTime = new Date().toISOString(); // Get current time in ISO format
push(ref(db, 'user/' + document.getElementById("userName").value), {
userName: document.getElementById("userName").value,
contents: document.getElementById("contents").value,
timestamp: currentTime // Add timestamp
});
alert("Success");
});
</script>
I'm writing in the realtime database using the push()
function
The push()
function orders the input data in chronological order, but it does not, and it orders the data in numerical order
I want the data to be entered in chronological order The userName is not based on timestamp, but the data is recorded in the order of 0107 after 0104
"0104": {
"-OKQu5uz3y6aq_exRrwA": {
"contents": "1",
"timestamp": "2025-03-03T12:08:34.229Z",
"userName": "0104"
}
},
"0107": {
"-OHJRj4ye8DOxw0UbEo1": {
"contents": "1",
"timestamp": "2025-01-23T18:35:48.476Z",
"userName": "0107"
}
},
"0107": {
"-OJRQ1IGgYYgmk-ri7Ye": {
"contents": "1 ",
"timestamp": "2025-02-19T04:16:44.235Z",
"userName": "0107"
},
<script type="module">
import { initializeApp } from "https://www.gstatic/firebasejs/11.1.0/firebase-app.js";
import { getDatabase, ref, set, get, child, push } from "https://www.gstatic/firebasejs/11.1.0/firebase-database.js";
import { getAnalytics } from "https://www.gstatic/firebasejs/11.1.0/firebase-analytics.js";
const firebaseConfig = {My Firebase Info};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getDatabase(app);
document.getElementById("submit").addEventListener('click', function (e) {
e.preventDefault();
const currentTime = new Date().toISOString(); // Get current time in ISO format
push(ref(db, 'user/' + document.getElementById("userName").value), {
userName: document.getElementById("userName").value,
contents: document.getElementById("contents").value,
timestamp: currentTime // Add timestamp
});
alert("Success");
});
</script>
Share
Improve this question
edited Mar 4 at 14:17
Frank van Puffelen
601k85 gold badges890 silver badges860 bronze badges
Recognized by Google Cloud Collective
asked Mar 4 at 3:16
DebugDebug
12 bronze badges
4
|
1 Answer
Reset to default 0Data in the Firebase Realtime Database dashboard/console is always sorted lexicographical based on the keys. So your keys on the "0104"
level are sorted on that exact value (with "104" appearing before "107", etc).
There is no way to change the order how the Firebase console displays these values.
If the ability to see them in a different order is crucial during your development cycle, consider building a small custom dashboard where you use the ordering operations of the API.
Note though that in that case too, you won't be able to sort the "0104"
level of the data on the push keys or timestamp
values that you currently have, as each node on that "0104"
level must have exactly one value at a fixed path under it to sort it on.
As usual when dealing with NoSQL databases, you can work around this problem by adding some additional data. For example by adding a sortValue
under each such node:
"0104": {
"sortValue": "-OKQu5uz3y6aq_exRrwA", //
本文标签:
javascriptFirebase realtimedatabase sort to timeStack Overflow
版权声明:本文标题:javascript - Firebase realtimedatabase sort to time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745062914a2640342.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
-OKR
nodes seem correct to me, as-OKU6l
comes before-OKY6m
alphabetically. – Frank van Puffelen Commented Mar 4 at 14:16