Closed BryanEnid closed 4 years ago
Do you use the get
method or the onSnapshot
listener? Either way I'm thinking that the database isn't indexed to support this and the error isn't surfacing. Can you try a catch
statement if you're using get
or a error handler function for onSnpashot
?
Hey, thanks for replying back. I have used onSnapshot
. There is no error whatsoever. It's just that it doesn't do the query as specified. It doesn't get the data back.
const firestore = firebase.firestore();
const GeoFirestore = geofirestore.initializeApp(firestore);
const geoCollection = GeoFirestore.collection("restaurants");
// Queries NOT WORKING when using both at the same time
let query = geoCollection
.near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: radius })
.where("status", "==", "active");
const unsubscribe = query.onSnapshot((res) =>
res.docChanges().forEach((change) => {
switch (change.type) {
case "added":
console.log(change.doc.data()); // I"M NOT GETTING DATA and there is no error.
return addJob(change.doc.id, change.doc.data());
case "modified":
return updateJob(change.doc.id, change.doc.data());
case "removed":
return removeJob(change.doc.id);
default:
break;
}
});
});
But if I do this instead:
const firestore = firebase.firestore(); // Create a Firestore reference
const GeoFirestore = geofirestore.initializeApp(firestore); // Create a GeoFirestore reference
const geoCollection = GeoFirestore.collection("restaurants"); // Create a GeoCollection reference
// =========================================================
let query = geoCollection
// .near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: radius })
.where("status", "==", "active");
// OR
let query = geoCollection
.near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: radius });
// .where("status", "==", "active")
// ==========================================================
const unsubscribe = query.onSnapshot((res) =>
res.docChanges().forEach((change) => {
switch (change.type) {
case "added":
console.log(change.doc.data()); // I DO GET THE DATA <------------------------------------
return addJob(change.doc.id, change.doc.data());
case "modified":
return updateJob(change.doc.id, change.doc.data());
case "removed":
return removeJob(change.doc.id);
default:
break;
}
});
});
Here is the structure of firebase:
Hey @BryanEnid I hope you're closing the issue because you were able to resolve the issue, out of curiosity what was the solution?
Hey, @MichaelSolati. Yeah, I could solve It. It was all my fault. First of all, thanks for helping me out and giving maintenance to such a great tool.
I had to create an Index in the Firestore Console to be able to use where()
and near()
together. I found this when I actually tried to console log doc.data()
with the get()
method and I got a response with the next log:
[FirebaseError: The query requires an index. You can create it here: (SOME URL)]
But one thing is certain, I didn't get this message with the onSnapshot
response.
[REQUIRED] Describe your environment
firebase
,firebase-admin
, etc...) and version: firebase 7.9.0[REQUIRED] Describe the problem
GeoQuery Instance was returned but it cannot get the provided data from firestore
Steps to Reproduce
Relevant Code
This works:
Also this:
But not together: