Cloud functions returns null, when called directly from the app

问题: 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...

问题:

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


回答1:

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);
    })
});
  • 发表于 2019-03-22 18:02
  • 阅读 ( 209 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除