javascript - 结合 limitToFirst() 和 limitToLast() 会导致意外行为
问题描述
我在实时 Firebase 数据库上有以下数据集:
{
"posts": {
"key1": {
message: "message",
title: "title",
timeStamp: 1513160947658
},
"key2": {
message: "message",
title: "title",
timeStamp: 1625904019915
},
"key3": {
message: "message",
title: "title",
timeStamp: 1626171405405
}
}
}
这是 Firebase 组件:
Firebase.js
import { get, getDatabase, limitToFirst, limitToLast, onValue, orderByChild, query, ref } from 'firebase/database'
import firebase from 'firebase/compat/app'
import 'firebase/compat/database'
const config = { ... }
const db = getDatabase()
firebase.initializeApp(config)
export { db, get, limitToFirst, limitToLast, onValue, orderByChild, query, ref }
组件获取第一个和最后一个帖子:
FirstAndLast.js
import { db, get, limitToFirst, limitToLast, orderByChild, query, ref } from './Firebase'
const fetch = async () => {
// If I execute both lines, Listener.js snapshot.val() returns a null value
// If I remove one of these lines, Listener.js snapshot.val() returns the desired object
let snapshotFirst = await get(query(ref(db, 'posts'), orderByChild('timeStamp'), limitToFirst(1)))
let snapshotLast = await get(query(ref(db, 'posts'), orderByChild('timeStamp'), limitToLast(1)))
}
另一个组件监听key2
分支:
Listener.js
import { db, onValue, ref } from './Firebase'
onValue(ref(db, 'posts/key2'), snapshot => {
console.log(snapshot.val())
})
如果我同时执行两个组件,Listener.js
首先打印所需的对象,但稍后返回一个null
值作为最终结果。如果我在 删除(或评论)其中一张快照FirstAndLast.js
,则没有任何问题。
组合limitToFirst()
和有什么问题limitToLast()
吗?
解决方案
推荐阅读
- javascript - Javascript/CSS 灯箱仅适用于第一个元素?
- javascript - vue.js VeeValidate - 自定义验证器正确编译错误但不切换错误类
- android - Appium (Android):在设备上运行 1 次测试后启动超时
- c - 如何规避 GCC 的“尝试使用中毒的 malloc/calloc”错误?
- c++ - 指针无效?
- reactjs - 错误:将语义 UI 与 Webpack 4 结合时做出反应
- python - 从其他两个字典的差异中创建字典的最佳方法是什么?
- c# - 如何创建自己的 Raycast 组件,它不需要碰撞器,但在 C# Unity3D 中支持 Skinned Mesh Renderer?
- visual-studio - TeamCity Visual Studio 测试构建步骤以运行单个测试
- android - 适用于 Android 的 Firestore getAll 等效项