Shun Yamada
Shun Yamada

Reputation: 979

Error: Query.get failed: First argument must be an object

Using Firestor on React Native.

I am getting this error:

Error: Query.get failed: First argument must be an object

When I try to fetch User data from other collection(Item), but that would not work.

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);
    })
  };
};

I could get data snapshot but I cannot realize still.

Upvotes: 0

Views: 950

Answers (2)

Ronnie Smith
Ronnie Smith

Reputation: 18575

See QuerySnapshot documentation. The .where is optional.

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}`);
  }
});

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317487

get() doesn't take an argument like this:

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

If you want to fetch a document by ID, you need to build a DocumentReference using doc(), and call get() on it:

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

That will return a promise the yields a single DocumentSnapshot with the contents of that document.

Upvotes: 2

Related Questions