I'm fetching longitude and latitude values from a database using a home address.
The structure of each of the documents in the collection is as follows:
"Ext": string
"Lat": string
"Long": string
"Parish": string
"Postal": string
"Street": string
"Number": string
There are a handful of different where() conditions, all wrapped in if statements.
The street number where() condition always causes the query to return no matches, and all of the other where()'s find matches correctly.
I have tried:
- commenting out the street number where() condition
- hard coding values copied from the database
- using parseInt() and toString() in the where() call to force typing
- logging data about the string to the console to check typing, leading/trailing whitespace, etc.
function addressToCoords() {
// returns an array of coordinates if successful, false otherwise
const ADD = document.getElementById("address").value;
let NUM;
let STREET;
if (ADD) {
NUM = ADD.match(/^[0-9]+/)[0];
STREET = ADD.replace(/^[0-9]+\s/, "");
if (NUM === STREET) {
console.log("NUM = STREET. regex split failed");
STREET = null;
return false;
}
}
const CTY = document.getElementById('parish').value;
const POS = document.getElementById('postalCode').value.replace(/\s/, "");
let coords = [0.0, 0.0];
let query = f.addresses;
if (CTY) { console.log("CTY: ", CTY); query = query.where("Parish", "==", CTY); }
if (STREET) { console.log("STREET: ", STREET); query = query.where("Street", "==", STREET); }
if (POS) { console.log("POS: ", POS); query = query.where("Postal", "==", POS); }
// -FIXME- doesn't match
if (NUM) {
// Usual test case outputs:
// NUM: 1 string 1
console.log("NUM: ", NUM, " ", typeof(NUM), " ", NUM.length);
query = query.where("Number", "==", NUM);
}
query.limit(5)
.get()
.then(querySnapshot => {
if (!querySnapshot.empty) {
querySnapshot.docs.forEach (ele => {
console.log("doc found!: ", ele.data());
});
coords[0] = querySnapshot.docs[0].Lat;
coords[1] = querySnapshot.docs[0].Long;
} else {
console.log("no doc found!");
}
}).catch(function(error) {
console.log(error);
});
document.getElementById("Latitude").value = coords[0];
document.getElementById('Longitude').value = coords[1];
return coords;
}
Any insight is appreciated.
Edit: I'm currently also going back and forth with firebase support, and apparently my code successfully queries the example database the rep built and functions correctly.
That seems to leave some kind of issue with the database structure, but...
- I'm not querying to a different collection
- All documents have the same fields with the same types
- The testing I've done doesn't seem to allow the possibility of a type mismatch in the query
This is probably my ignorance talking, but the problem seems pretty bizarre.