My goal is to incorporate Firebase cloud functions into my app for posting user emails and names to a Firestore collection with randomly generated IDs. While everything seems to be functioning properly, I am facing an issue in receiving a response from the function back to my app. Below is the snippet of code from my app where I invoke the cloud function:
onPress ({commit, dispatch}, payload) {
var addUserInformation = firebase.functions().httpsCallable('addUserInformation');
addUserInformation(payload)
.then(function(result) {
console.log(result)
}).catch(function(error) {
var code = error.code;
var message = error.message;
var details = error.details;
console.log(code);
console.log(message);
console.log(details);
});
},
And here's the implementation of the cloud function:
const functions = require('firebase-functions')
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.addUserInformation = functions.https.onCall((data) => {
admin.firestore().collection('Backend').where('email', '==', data[1]).get()
.then(function(querySnapshot) {
if (querySnapshot.size > 0) {
console.log('Email already exists')
} else {
admin.firestore().collection('Backend').add({
name: data[0],
email: data[1]
})
console.log('New document has been written')
}
return {result: 'An outcome goes here'};
})
.catch(function(error) {
console.error("Error adding document: ", error);
})
});
The console displays that the result is null