简体   繁体   English

Firestore 查询在 react-native 中不起作用

[英]A firestore query not working in react-native

I am using react-native and I want to get specific data so I used a query, the problem is that it didn't work, but if I remove the where and do an if statement it works.我正在使用 react-native 并且我想获取特定数据所以我使用了查询,问题是它不起作用,但是如果我删除 where 并执行 if 语句它就可以工作。 How can I fix this?我怎样才能解决这个问题?

This is my implementation:这是我的实现:

let query = firestore().collection('conversations');
query = query.where('users', 'array-contains', myId);
// query = query.where('postId', '==', postId);
query
  .orderBy('timestamp', 'desc')
  .limit(limit)
  .get()
  .then((snapshot) => {
    const offersObjects = {};
    if (snapshot.docs.length) {
      snapshot.docs.forEach((doc) => {
        if (postId === doc.data().postId) {
          offersObjects[doc.id] = { ...doc.data(), pendingMessages: [] };
        }
      });
      dispatch({
        type: chatTypes.SET_OFFERS,
        payload: { offers: offersObjects },
      });
    }
  })

Where the code is commented is where this query doesn't work which is weird since the one above it works fine.代码被注释的地方是这个查询不起作用的地方,这很奇怪,因为上面的查询工作正常。 How can I fix this?我怎样才能解决这个问题?

If you have more than one condition on a query, it may need a composite index that is not automatically created.如果一个查询有多个条件,它可能需要一个不会自动创建的复合索引。 If that is the case, executing the query (eg by calling get() on it) will raise an error, but you'r not hancling errors.如果是这种情况,执行查询(例如通过对其调用get() )将引发错误,但您并没有处理错误。 I recommend adding a catch clause after your then and logging the error.我建议在then之后添加一个catch子句并记录错误。

If the problem is caused by a missing index, the error message contains a URL that opens the Firebase console on the page to create the exact index that is needed, with all fields already filled in. Create the index, wait for it to be completely created, and run the query again.如果问题是由于缺少索引引起的,错误消息中包含一个 URL 会在页面上打开 Firebase 控制台创建所需的准确索引,所有字段都已填写。创建索引,等待它完成创建,然后再次运行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM