I am trying to use Firebase cloud functions from my app to post emails and names of users into cloud firestore collection with randomly generated IDs. Everything works well, but I want to get a response from a function to my app and I can't really manage to do that. Here's my code from my app where I call 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 code of a 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: 'Something here'};
})
.catch(function(error) {
console.error("Error adding document: ", error);
})
});
The console shows that result is null