【问题标题】:Firebase task.getResult().getData(); always returning null. (asynchronous problem?)Firebase task.getResult().getData();总是返回 null。 (异步问题?)
【发布时间】:2020-09-02 22:47:06
【问题描述】:

我的猜测是存在问题,因为我没有返回正确的承诺? 任何帮助将不胜感激,在此先感谢。 这是我的程序任务:

(...)                 
                    .continueWith(new Continuation<HttpsCallableResult, String>() {
                        @Override
                        public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                            if(task.isSuccessful()) {
                               Map<String, Object> result = (Map<String, Object>) task.getResult().getData();//<-- this always returns null
                                return (String) result.get("userID");
                            }
                            else return "Task FAILED";
                        }
                    });
        }

这是我的云代码:

export const createCompany = functions.https.onCall((data,context) => {

    const email = data.email //data.key
    const password = data.password

    admin.auth().createUser({
        email: email,
        emailVerified: true,
        password: password,
        disabled: false
    })
    .then(async function(userRecord) {
        // See the UserRecord reference doc for the contents of userRecord.
        return setAsCompany(email).then(() =>{
           console.log("Company Role Granted", userRecord.email);
           return {userID : userRecord.uid};
       })
    })
    .catch(function(error) {
        console.log("Error creating new user:", error);
    });

  });

  async function setAsCompany(email: string): Promise<void>{
    const user = await admin.auth().getUserByEmail(email);
    console.log("ADDED AS COMPANY");

        return admin.auth().setCustomUserClaims(user.uid,{
            company: true,
        });
}

【问题讨论】:

    标签: typescript firebase asynchronous async-await google-cloud-functions


    【解决方案1】:

    确实,您没有正确返回 Promises 链。您还可以混合使用 async/awaitthen() 方法调用,这不是真正推荐的。以下应该可以解决问题:

    export const createCompany = functions.https.onCall(async (data, context) => {
    
        try {
    
            const email = data.email //data.key
            const password = data.password
    
            const userRecord = await admin.auth().createUser({
                email: email,
                emailVerified: true,
                password: password,
                disabled: false
            });
    
            await admin.auth().setCustomUserClaims(userRecord.uid, {
                company: true,
            });
    
            return { userID: userRecord.uid };
    
        } catch (error) {
            // See https://firebase.google.com/docs/functions/callable#handle_errors
        }
    
    });
    

    【讨论】:

    • 感谢您的帮助,它成功了,现在我有一个功能可以在创建用户时触发并覆盖那些自定义声明,我该如何防止这种情况发生?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多