Firebase Firestore "Order by time"

Firebase Firestore "Order by time"

我正在尝试实施 firebase 数据库 "order by timestamp" 同步但无法解决。

对我来说,它不会在 "any order"

中保存消息

以下是我的同步代码块:

let time =  {time: moment().format('MM Do YY, h:mm: a')}
const createMessage = (uid, text, displayName) => ({
  uid,
  text,
  displayName,
  time
})

function * syncMessagesSaga () {
    //ordering by uid does not make sense to me
    const channel = yield call(()=>prsf(({rsf})=>
     rsf.firestore.channel('messages')));
    while(true) {
        const snapshot = yield take(channel);
        let messages = [];
        snapshot.forEach(message => {
            messages.push({id: message.id, ...message.data()})  
        });
        yield put(syncMessages(messages))

      }
}

问题是我必须放入以下代码块

.orderBy("time", "asc")

但我不知道在哪里添加

我在想可能是我的发码功能没有按顺序保存 我发送和保存的代码块:

function * sendMessageSaga (action) {
  const rsf= yield call(getRsf);
  const uid = yield select(state => state.login.user.uid)
  const displayName = yield select(state => state.login.user.displayName)

  yield call(rsf.firestore.addDocument, 'messages', createMessage(uid, action.message, displayName))
  yield put(reset('DCTForm'))
  yield put(reset('MessageForm'))
  yield put(changeNewMessage(''))
}

所以在进行了一些头脑风暴之后,我找到了解决问题的方法:- 我使用了一个具有数组排序功能的函数: 代码块:-

function compareValues(key, order = 'asc') {
      return function (a, b) {
        if (!a.hasOwnProperty(key) ||
          !b.hasOwnProperty(key)) {
          return 0;
        }
        const varA = (typeof a[key] === 'string') ?
          a[key].toUpperCase() : a[key];
        const varB = (typeof b[key] === 'string') ?
          b[key].toUpperCase() : b[key];

        let comparison = 0;
        if (varA > varB) {
          comparison = 1;
        } else if (varA < varB) {
          comparison = -1;
        }
        return (
          (order == 'desc') ?
            (comparison * -1) : comparison
        );
      };
    }
    yield put(syncMessages(messages.sort(compareValues('time'))))

如果你愿意,请告诉我正确的解决方法,到那时我会使用它 谢谢