admin管理员组

文章数量:1202372

Here's a picture of my data:

I'm trying to get that document. This works:

var docRef = db.collection('users').doc('jPDKwHyrFNXNTFI5qgOY');
docRef.get().then(function(doc) {
  if (doc.exists) {
    console.log("Document data:", doc.data());
  } else {
    console.log("No such document!");
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});

It returns:

I.e., if I know the document's key I can get the document.

This doesn't work:

db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
  if (querySnapshot.exists) {
    console.log(querySnapshot.data);
  } else {
    console.log("No such document!");
  }
})
.catch(function(error) {
  console.log("Error getting document: ", error);
});

It just returns No such document! What's wrong with my query?

Here's a picture of my data:

I'm trying to get that document. This works:

var docRef = db.collection('users').doc('jPDKwHyrFNXNTFI5qgOY');
docRef.get().then(function(doc) {
  if (doc.exists) {
    console.log("Document data:", doc.data());
  } else {
    console.log("No such document!");
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});

It returns:

I.e., if I know the document's key I can get the document.

This doesn't work:

db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
  if (querySnapshot.exists) {
    console.log(querySnapshot.data);
  } else {
    console.log("No such document!");
  }
})
.catch(function(error) {
  console.log("Error getting document: ", error);
});

It just returns No such document! What's wrong with my query?

Share Improve this question edited Oct 11, 2017 at 16:41 Scarygami 15.5k2 gold badges36 silver badges24 bronze badges asked Oct 11, 2017 at 16:12 Thomas David KehoeThomas David Kehoe 10.9k18 gold badges75 silver badges122 bronze badges 1
  • Please tag with the languages used for code. This is JavaScript. – tadman Commented Oct 11, 2017 at 16:13
Add a comment  | 

2 Answers 2

Reset to default 25

The difference in your two requests is that in the first case you are retrieving one document which gives you a DocumentSnapshot which has the exists property and the data() method.

In the second case you do a query, which gives you a QuerySnapshot that has to be handled differently from a DocumentSnapshot. Instead of a single document you get a list/collection of documents. You can check if data has been retrieved using the empty or size properties, and then go through the results using the forEach method or going through the docs array:

db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
  if (querySnapshot.size > 0) {
    // Contents of first document
    console.log(querySnapshot.docs[0].data());
  } else {
    console.log("No such document!");
  }
})
.catch(function(error) {
  console.log("Error getting document: ", error);
});

I had the same problem with my project. First of all there have to be an object in your database that match with the .where('objectid',==, 'object.id') then it is possible to get your data to loop. Like querysnapshot.docs.forEach((d) => d.data());

本文标签: javascriptFirebase Cloud Firestore query not finding my documentStack Overflow