【问题标题】:Error: Query.get failed: First argument must be an object错误:Query.get 失败:第一个参数必须是一个对象
【发布时间】:2018-08-12 03:06:05
【问题描述】:

在 React Native 上使用 Firestor。

我收到此错误:

错误:Query.get 失败:第一个参数必须是对象

当我尝试从其他集合(项目)中获取用户数据时,这不起作用。

export const itemsFetch = () => {
  return (dispatch) => {
    firebase.firestore().collection('items').get()
    .then((snapshot) => {
      const items = snapshot.docs.map(doc => doc.data());
      return items
   })
    .then((items) => {
      const userPromises = items.map((item, itemId)  => {
        const userId = item.userId;
        console.log('userId: ', userId);
        return firebase.firestore().collection('users').get(userId)
      .then((snapshot) => {
        console.log('snapshot:', snapshot);
        const user = snapshot.docs.data();
        return user;
        console.log('user:', user);
      })
      .then(user => ({...item, user: user, itemId}));
      });
      dispatch({ type: 'ui/loading/hide' });
      return Promise.all(userPromises);
    })
  };
};

我可以获取数据snapshot,但我仍然无法实现。

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore


    【解决方案1】:

    get() 不接受这样的参数:

    return firebase.firestore().collection('users').get(userId)
    

    如果你想通过ID获取一个文档,你需要使用doc()构建一个DocumentReference,然后调用get():

    return firebase.firestore().collection('users').doc(userId).get()
    

    这将返回一个 Promise,生成一个包含该文档内容的 DocumentSnapshot。

    【讨论】:

    • 感谢您的评论。但是发生了另一个错误TypeError: relativePath.split is not a function。我正在尝试获取 dabatase key = userId。
    【解决方案2】:

    请参阅QuerySnapshot 文档。 .where 是可选的。

    let query = firestore.collection('col').where('foo', '==', 'bar');
    
    query.get().then(querySnapshot => {
      let docs = querySnapshot.docs;
      for (let doc of docs) {
        console.log(`Document found at path: ${doc.ref.path}`);
      }
    });
    

    【讨论】:

    • OP 没有使用 where 子句。
    • ...它只是在文档中。这是可选的。概念/代码示例应该为他提供他需要的东西。
    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2021-12-22
    • 2018-08-20
    相关资源
    最近更新 更多