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 ({mit, 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
// //
//
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
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 ({mit, 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./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
Share Improve this question edited Mar 21, 2019 at 13:48 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Mar 21, 2019 at 13:43 Tadas MaksimavičiusTadas Maksimavičius 534 bronze badges1 Answer
Reset to default 9You're not returning a promise from the top-level of your Cloud Functions code, which means that the code ends without returning anything to the caller.
To fix this, return the value of the top-level get:
exports.addUserInformation = functions.https.onCall((data) => {
return 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);
})
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745196395a4616110.html
评论列表(0条)