Firebase query does not return anything

Leo S

I coded a function for sending a query to my database and retrieve the result. But if it does not find anything in the database it returns nothing neither undefined nor null.

For this reason, I can not handle whether the data exists in the database or not.

What is the return value of this function?


Code:

usersRef
  .where("nickname", "==", nickname)
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) =>
      doc.data() ? console.log("a") : console.log("b")
    );// i wrote this line for testing the issue
    querySnapshot.size == 0 ? (flag = true) : (flag = false);
  })
  .catch((err) => console.log("err2"))

If the data exists in the database, it prints a, but if not, it prints nothing, neither a nor b.

I used that code in my React-Native project, it also uses a Firebase Firestore database and usersRef means:

const db = firebase.firestore();
const usersRef = db.collection("Users");

Note: the querySnapshot always return a firebase object in JSON format. The doc parameters in forEach returns an object if it exists in JSON format but, if not, it cannot be handled.

Note 2: I also tried doc.data()== null or doc.data()== undefined or doc.data()== "". But they do not print anything in console.log(). I also tried console.log(doc.data()) and nothing changed.

Jeff McMahan

if the data exists in database, it prints a but if it does not exist it prints nothing neither a nor b

That's because you're querying the database and getting back an array. If your array has 1 or more items in it, your .forEach call will iterate through them and execute your ternary expression, resulting in a console log call - either a or b. However, if your array is empty, then .forEach won't have anything to iterate over, and so the function you're passing to it will never be executed.

[].forEach(item => console.log(item)) // nothing
[1].forEach(item => console.log(item)) // 1

So the issue is that your database query is just coming back empty on your Windows box. Check your flag variable or the .size member and you should see whether this is the case:

usersRef
  .where("nickname", "==", nickname)
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) =>
      doc.data() ? console.log("a") : console.log("b")
    );
    querySnapshot.size == 0 ? (flag = true) : (flag = false);
    console.log(querySnapshot.size);
  })
  .catch((err) => console.log("err2"))

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

ngFor it does not return anything

分類Dev

Does string("abc") return anything?

分類Dev

Assembly - Program does not return anything

分類Dev

This query wont return anything, i need advice on this

分類Dev

Angular 2 Firebase Observable to promise doesn't return anything

分類Dev

What does an MDX query return?

分類Dev

`INTERSECT` does not return anything from two tables, separately values are returned fine

分類Dev

Return anything in async move block

分類Dev

How can I use a Firebase Query to return a boolean?

分類Dev

Why Does SQL Len Function Return 7 for this Query?

分類Dev

sql query to spark-cassandra cluster thriftserver does not return

分類Dev

postgresql trigger with dblink doesn't return anything

分類Dev

Why do OpenGL methods not return anything?

分類Dev

User data not returning anything from query call

分類Dev

SPARQL query result not showing anything from graph

分類Dev

LINQ to XML query Where() function not returning anything

分類Dev

PHP cli script does not output anything

分類Dev

Why does foreach not bring anything to the driver program?

分類Dev

Template in union member - Declaration does not declare anything

分類Dev

Non blocking CherryPy does not receive anything

分類Dev

Does Bcrypt require anything to make it secure

分類Dev

My query to retrieve events between 2 search dates does not return any results, why?

分類Dev

firebase return onSnapshot promise

分類Dev

Return value of updateEmail() - firebase

分類Dev

Firebase limitToLast Query

分類Dev

Does the "?." operator do anything else apart from checking for null?

分類Dev

Does the "?." operator do anything else apart from checking for null?

分類Dev

write_analog in microPython on BBC micro:bit does not do anything

分類Dev

Changing SVG color (with object tag) in CSS and/or javascript, does not do anything

Related 関連記事

  1. 1

    ngFor it does not return anything

  2. 2

    Does string("abc") return anything?

  3. 3

    Assembly - Program does not return anything

  4. 4

    This query wont return anything, i need advice on this

  5. 5

    Angular 2 Firebase Observable to promise doesn't return anything

  6. 6

    What does an MDX query return?

  7. 7

    `INTERSECT` does not return anything from two tables, separately values are returned fine

  8. 8

    Return anything in async move block

  9. 9

    How can I use a Firebase Query to return a boolean?

  10. 10

    Why Does SQL Len Function Return 7 for this Query?

  11. 11

    sql query to spark-cassandra cluster thriftserver does not return

  12. 12

    postgresql trigger with dblink doesn't return anything

  13. 13

    Why do OpenGL methods not return anything?

  14. 14

    User data not returning anything from query call

  15. 15

    SPARQL query result not showing anything from graph

  16. 16

    LINQ to XML query Where() function not returning anything

  17. 17

    PHP cli script does not output anything

  18. 18

    Why does foreach not bring anything to the driver program?

  19. 19

    Template in union member - Declaration does not declare anything

  20. 20

    Non blocking CherryPy does not receive anything

  21. 21

    Does Bcrypt require anything to make it secure

  22. 22

    My query to retrieve events between 2 search dates does not return any results, why?

  23. 23

    firebase return onSnapshot promise

  24. 24

    Return value of updateEmail() - firebase

  25. 25

    Firebase limitToLast Query

  26. 26

    Does the "?." operator do anything else apart from checking for null?

  27. 27

    Does the "?." operator do anything else apart from checking for null?

  28. 28

    write_analog in microPython on BBC micro:bit does not do anything

  29. 29

    Changing SVG color (with object tag) in CSS and/or javascript, does not do anything

ホットタグ

アーカイブ