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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…