I’m trying to find duplicate usernames in the database when a new user registers before a user is authenticated using Firebase Cloud Functions as I was suggested, as the .queryEqual(toValue)
locally won’t work every time. What I can’t get my head around now is how to make use of it in my app. Here’s the cloud function I deployed:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.uniqueUsername = functions.https.onRequest((req, res) => {
const username = req.query.username
admin.database().ref('users').orderByChild('username').equalTo(username).once('value').then(snap => {
// if the child exists, then the username is taken
if (snap.exists()) {
res.send('username not available');
} else {
res.send('username available');
}
});
});
The equalTo(username)
is a value picked from UITextField and then compared to in the database. The error I get here is:
Error: Query.equalTo failed: First argument contains undefined in property 'users'
at Error (native)
at Ae (/user_code/node_modules/firebase-admin/lib/database/database.js:105:67)
at ze (/user_code/node_modules/firebase-admin/lib/database/database.js:104:400)
at W.h.Jf (/user_code/node_modules/firebase-admin/lib/database/database.js:142:60)
at exports.uniqueUsername.functions.https.onRequest (/user_code/index.js:10:60)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47)
at /var/tmp/worker/worker.js:649:7
at /var/tmp/worker/worker.js:633:9
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
And the error I get when I use the URL in the browser is:
Error: could not handle the request
What am I doing wrong? And what should I do next?