【发布时间】:2022-02-10 06:06:28
【问题描述】:
我在向 Firebase 实时数据库添加消息时尝试包含时间戳。我发现发布了许多类似的问题,但所提出的解决方案都没有奏效。在我尝试添加时间戳之前,我想知道我是否做错了什么,也许是我的配置或其他东西。
这是我的配置:
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js';
import { getDatabase, ref, push, set, onValue, query, orderByChild } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-database.js";
const firebaseConfig = {
...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const database = getDatabase(app);
当我尝试将消息添加到我的 messages 集合时,这是我的代码:
const messageListRef = ref(database, 'messages');
const newMessageRef = push(messageListRef);
set(newMessageRef, {
'name': name.value,
'message': message.value,
'createdAt': firebase.database.ServerValue.TIMESTAMP
});
我收到的错误:
index.js:31 Uncaught ReferenceError: firebase is not defined
at HTMLButtonElement.<anonymous>
我也试过了:
- app.database.ServerValue.TIMESTAMP
- database.ServerValue.TIMESTAMP
还有很多我不记得了。
非常感谢任何帮助。
【问题讨论】:
-
附带说明,您在此处使用的 Firebase Web v9.0.0 于 2021 年 8 月发布。您应该从 2022 年 2 月起将其更新到 v9.6.6。查找最新版本的简单方法CDN url 使用的数字是访问
firebaseon npm,记下右边的版本号并扔到 CDN url 中。
标签: firebase firebase-realtime-database timestamp