- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以使用普通的 Firestore 查询设置 redux-observable
export const getStatementsEpic = (action$, store) => {
return action$.ofType(GET_STATEMENTS)
.filter(() => {
const state = store.getState()
return state.auth.user
})
.mergeMap(() => {
console.log('action', store.getState().auth.user.uid)
const db = firebase.firestore()
db.settings({ timestampsInSnapshots: true })
const query = db.collection('users')
.doc(store.getState().auth.user.uid)
.collection('statements')
.orderBy('uploadedOn', 'desc')
.limit(50)
return query.get().then(snapshot => {
console.log('Should have gotten snapshot')
return getStatementsSnapshot(snapshot)
})
})
}
但我想将其转换为实时,我尝试更改
return query.get().then(snapshot => {
到
return query.onSnapshot(snapshot => {
但是它不起作用......我想这不是一个 promise ?我该如何解决这个问题?
最佳答案
你是对的,onSnapshot
方法不会返回 promise 。相反,它返回一个可用于取消订阅更改通知的函数。在调用取消订阅函数之前,每次文档更改时都会调用传递给 onSnapshot 方法的回调。 (documentation 表示回调也将立即使用当前文档内容调用。)
像onSnapshot
这样多次使用回调函数的函数可以使用fromEventPattern
函数“转换”为可观察对象。 fromEventPattern
采用两个函数作为参数。您传递的第一个函数需要调用 onSnapshot
,并将 RxJS 定义的处理程序作为回调传递给它。您传递的第二个函数需要调用 onSnapshot
返回的取消订阅函数。当您订阅可观察对象时(即在您的史诗中使用它),RxJS 将调用第一个函数。当您取消订阅可观察对象时,RxJS 将调用第二个函数。
以下是更新为使用 fromEventPattern
和新 RxJS 管道的代码示例:
export const getStatementsEpic = (action$, state$) => action$.pipe(
ofType(GET_STATEMENTS),
withLatestFrom(state$),
filter(([action, state]) => state.auth.user),
mergeMap(([action, state]) => {
const db = firebase.firestore()
db.settings({ timestampsInSnapshots: true })
return fromEventPattern(
handler => db.collection('users')
.doc(state.auth.user.uid)
.collection('statements')
.orderBy('uploadedOn', 'desc')
.limit(50)
.onSnapshot(handler),
(handler, unsubscribe) => unsubscribe(),
).pipe(
map(getStatementsSnapshot),
takeUntil(action$.pipe(
ofType(STOP_GET_STATEMENTS),
)),
)
}),
)
请注意,我已将 takeUntil
引入到快照流中。没有它(或类似的东西),快照流将永远不会结束。另一个可能的更改是使用 switchMap
而不是 mergeMap
。如何取消订阅仅取决于您的用例。
关于reactjs - 如何将 Firestore 实时更新(onSnapshot)与 redux-observable/rxjs 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50655236/
我正在建模数据并且有一个关于 onSnapshot 的问题(网络)听众。正如在 SO 上的几篇文章和文档中指出的那样,在初始调用之后,监听器仅获取更改的数据。我很想知道更改的数据是什么。如果监听文档,
简单版本:当我在Firestore中注册onSnapshot时,有没有办法知道我是否是推送更新的客户端? 详细版本: 打开网页时,它会将 onSnapshot 回调注册到名为 notes 的 Fire
我的 firestore onSnapshot() 函数被调用两次。 let user = firebase.firestore().collection('users').doc(userID).o
我正在使用 Firestore 的 onSnapshot() 方法来监听约 5,000 个文档的集合。在传递给 onSnapshot 的回调中,我获取快照并将文档格式化为数组和对象。 db.onSna
我正在尝试使用 Firebase->firestore 与 web 建立聊天,因此我尝试将其与 onSnapshot 一起使用,以便用户在打开聊天时能够收到消息。 我的 noSQL 数据库结构如下:
我有一个带有多个路由和 vuex 的 vue-cli-4 应用程序。 Firestore 数据库已成功连接 - 我的应用程序立即反射(reflect)从 Firestore 控制台应用于数据库的修改。
我有用户...每个用户都有多个他/她所属的组。每个用户组成员都是一个嵌入在他/她的用户文档中的数组。有一个任务集合,每个任务包含一个与用户组相对应的 authorizedGroups 数组。大多数任务
我有一个从 Firestore 获取数据的函数,但总是返回未定义的。我以前使用过 .get() 方法,但我希望在数据库获取一些新数据时自动更新我的数据。 我知道 .onSnapshot() 不会返回
当我导航到某个页面时,我会传递一些参数,例如“折扣”,并且我会在 render() 中显示该数字,但我正在调用onSnapshot() 就在导航到该类的开始处。现在,我想在开头仅显示参数的值,并在发生
我需要在页面上放置一个大约 20 个 Vue 组件。每个组件在挂载时都会为自己创建一个 onSnapshot,相应地,当它被删除时会创建一个 unsubscribe()。 const ref = fi
在我的网络应用程序中,我发起了一个查询以收集用户最近的所有消息,然后使用 onsnapshot 在提要中显示这些消息。在我的应用程序中,一些消息会发布到 future ,所以我只想要来自现在或更早的消
我正在设计一个包含(很多)文档的应用程序,这些文档共享给(可能很多)组。 我需要了解如何将文档更改从服务器推送到客户端,以便构建我的数据模型。 是否将对数据库的所有更改都推送到客户端(使用最少的数据)
我正在构建一个简单的 firebase 应用程序。主文本区域链接到 Firebase 上的条目。当用户键入时,我反跳 keyup 事件(300 毫秒),并在 Firebase 上进行更新。 每个 .u
我目前正在 React 中创建一个待办事项列表,它从 Firestore 检索任务并使用状态 Hook 将它们本地存储在一个数组中:const [todoList, setTodoList] = us
我正在 React Native 中从 firestore 获取项目列表。 如果更新了项目文档,我的列表会通过 onSnapshot 按预期刷新。但是如果文档被删除,我的列表不会刷新。 有没有办法让我
我创建了一个 Ionic Firebase 聊天应用程序。我认为我遇到的问题是我在消息页面初始化时设置了一个查询快照,如下所示: ngOnInit() { this.messageSe
documentation说: You can listen to a document with the onSnapshot() method. An initial call using the
所以基本上请告诉我应该如何在 React 的 onSnapshot() 中实现 firestore 的 orderBy() 函数。请帮助我,伙计们。我真的需要你的帮助。如果您回答了我的问题,请与其中的
所以基本上请告诉我应该如何在 React 的 onSnapshot() 中实现 firestore 的 orderBy() 函数。请帮助我,伙计们。我真的需要你的帮助。如果您回答了我的问题,请与其中的
有没有人知道如何在 firestore 中取出 onSnapshot 函数。我想如果 if 条件满足,函数将离开并且数据库请求将退出。该函数由 onClick 调用,第一步是在数据库中添加一些内容。第
我是一名优秀的程序员,十分优秀!