【问题标题】:Why does my firestore function return null to my app?为什么我的 firestore 函数返回 null 给我的应用程序?
【发布时间】:2020-03-28 00:21:43
【问题描述】:

我想弄清楚为什么我的 firestore 函数总是返回 null。 这是我的firestore函数:

exports.isUserOnListFunction = functions.https.onCall(async (data, context) => {
    const userID = context.auth.uid;
    const userEmail = context.auth.token.email;
    const atIndex = userEmail.indexOf('@');
    const dotIndex = userEmail.indexOf('.');
    var userName = userEmail.slice(0, dotIndex);
    var userSurname = userEmail.slice(dotIndex+1, atIndex);
    userName = capitalizeFirstLetter(userName);
    userSurname = capitalizeFirstLetter(userSurname);
    return db.collection('allowed_users')
        .where("name", "==", userName)
        .where("surname", "==", userSurname)
        .get().then(snap => {
            if (!snap.empty) {
                db.collection("users").doc(userID).update({
                    onList: true
                });
                return {onList : true};
            }
        }).catch(()=>{
            console.log("Some error!")
        })

});

这是我的调用 firestore 函数的 android (java) 函数:

    private Task<String> isUserOnList(){
    return mFunctions
            .getHttpsCallable("isUserOnListFunction")
            .call()
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    String result = (String) task.getResult().getData();
                    System.out.println(result);
                    return result;
                 }
            });
}

谁能告诉我我做错了什么?

【问题讨论】:

  • 你需要注意update返回的promise,否则你的函数可能会在工作完成之前提前终止。
  • 我做了一些研究,你是 100% 正确的。感谢您的时间! :)

标签: java android promise google-cloud-firestore google-cloud-functions


【解决方案1】:

据此doc

如果返回的是带有键和值的 JS 对象,我想是在这种情况下,getData() 返回 Map 对象。

我认为应该使用task.getResult().getData().toString()方法来转换它(而不是(String) task.getResult().getData();)

我预计会抛出异常,但也许您已经在此处未发布的其他代码部分中发现了它。

希望对你有帮助。

【讨论】:

  • 非常感谢您的回答! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 2022-09-29
  • 2022-01-10
  • 2020-06-06
  • 1970-01-01
相关资源
最近更新 更多