【问题标题】:Firebase Cloud Functions Firestore - Cannot read property 'text' of nullFirebase Cloud Functions Firestore - 无法读取 null 的属性“文本”
【发布时间】:2018-09-05 09:29:10
【问题描述】:

我正在尝试编写一个从文档中读取的谷歌云函数。

这个函数运行正常,可以返回值:

exports.helloWorld = functions.https.onRequest((request, response) => {
    var userArr = [];
    fs.collection("user")
        .where("user_id", "==", "qIXpbXTuJ5PQHm3rGuTeeSbdnWi1")
        .get()
        .then(querySnapshot => {
            querySnapshot.forEach(doc => {
                userArr.push(doc.data());
            });
            response.send(userArr);
        })
        .catch(err => {
            return err;
        });
});

但是这个返回错误:

服务器

exports.matches_people = functions.https.onCall((data, context) => {
    var userArr = [];
    fs.collection("user")
        .where("user_id", "==", "qIXpbXTuJ5PQHm3rGuTeeSbdnWi1")
        .get()
        .then(querySnapshot => {
            querySnapshot.forEach(doc => {
                userArr.push(doc.data());
            });
            return userArr;
        })
        .catch(err => {
            return err;
        });
});

客户

var matches_people = firebase.functions().httpsCallable('matches_people');
                matches_people({
                    user_id:  self.login.user_id
                }).then(function (result) {
                    // Read result of the Cloud Function.
                    var sanitizedMessage = result.data.text;
                    console.log(result);
                    // ...
                }).catch(function (error) {
                    // Getting the Error details.
                    var code = error.code;
                    var message = error.message;
                    var details = error.details;
                    console.log(error); //return error: TypeError: Cannot read property 'text' of null
                    // ...
                });

在 httpsCallable 上,它返回错误 TypeError: Cannot read property 'text' of null

请帮忙。

对不起我的英语

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    在 HTTP 可调用函数中,为了“在异步操作后返回数据,return a promise”,如文档 here 中所述。

    get() 方法是异步的并返回一个承诺,如 here 所述。

    所以你应该只返回get()方法返回的promise,如下:

    exports.matches_people = functions.https.onCall((data, context) => {
        var userArr = [];
        return fs.collection("user")   //  <- Note the return here
            .where("user_id", "==", "qIXpbXTuJ5PQHm3rGuTeeSbdnWi1")
            .get()
            .then(querySnapshot => {
                querySnapshot.forEach(doc => {
                    userArr.push(doc.data());
                });
                return userArr;
            })
            .catch(err => {
                return err;
            });
    });
    

    请注意,这与 HTTP 云函数不同,您应以 send()redirect()end() 结尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2020-03-27
      • 2020-02-01
      • 2018-11-06
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      相关资源
      最近更新 更多