javascript - Cloud functions returns null, when called directly from the app - Stack Overflow

I am trying to use Firebase cloud functions from my app to post emails and names of users into cloud fi

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 badges
Add a ment  | 

1 Answer 1

Reset to default 9

You'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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信