i'm trying to preform a Firestore query to check if an email already exits in the database. I want to do this with a simple function and than check if the function returns with false or true, but the if statements gives an output before the function is even fired can somebody help me?
const admin = require("firebase-admin");
// Initialize Firebase connection
var serviceAccount = require("./fbAccount.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://ancobook-1aceb.firebaseio.com"
});
let db = admin.firestore();
function checkEmail(test) {
let citiesRef = db.collection('users');
let query = citiesRef.where('username', '=', test).get()
.then(snapshot => {
if (snapshot.empty) {
console.log('No matching documents.');
return true;
}
snapshot.forEach(doc => {
console.log('Matching documents');
return false;
});
})
}
if (checkEmail('brancoschoenaker@gmail.com') == false) {
console.log('Account already exists')
} else {
console.log('Created Account')
}