Firebase Echtzeitdatenbank sortiert zur Zeit
Posted: 04 Mar 2025, 04:32
Ich schreibe in der Echtzeitdatenbank mit der Funktion push () funktion
Die Function ordnet die Eingangsdaten in chronologischer Reihenfolge, aber es nicht, und es ordnet die Daten in numerischer Reihenfolge. von 0107 nach 0104 < /p>
Die Function ordnet die Eingangsdaten in chronologischer Reihenfolge, aber es nicht, und es ordnet die Daten in numerischer Reihenfolge. von 0107 nach 0104 < /p>
Code: Select all
"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"
},
< /code>
Echtzeitdatenbank < /p>
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-app.js";
import { getDatabase, ref, set, get, child, push } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-database.js";
import { getAnalytics } from "https://www.gstatic.com/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");
});