【问题标题】:Firebase functions: if value exists, act then deleteFirebase 函数:如果值存在,则执行然后删除
【发布时间】:2020-01-10 13:52:30
【问题描述】:

我正在尝试通过在注册时通过邮件发送的链接来验证我的用户电子邮件地址。 当用户注册时,我在临时表中创建了一个指向其 uid 的引用,并发送一封带有链接的邮件以激活他的帐户。 这个链接调用我的firebase云函数,我检查参数中传递的值是否存在于临时表中,如果存在,我激活关联的帐户,然后删除临时表上的记录。

到目前为止我所拥有的:

exports.confirmMail = (data) => {
    return admin.database().ref(`/Email-Verifications/${data.uid}`).once('value')
    .then((snapshot) => {
        let uid = snapshot.val().userId;
        return new Promise((resolve, reject) => {
            admin.auth().updateUser(uid, { emailVerified : true })
            .then(()=>{
                admin.database().ref(`/Email-Verifications/${data.uid}`).remove()
                .then(()=>{
                    console.log('email verified');
                    return 'Ok';
                })
                .catch(()=>{
                    console.log(error);
                    return error;                    
                })
            })
        })        
        .catch((error)=>{
            console.log(error);
            return error;
        })
    })
    .catch((error)=>{
        console.log(error);
        return error;
    })
}

这有效(根据 firebase 控制台中的日志),但在我的前端我得到一个空对象,所以无法知道该过程是否真的有效。

我迷失了承诺返回,我不得不通过return new Promise (...) 更改 .then 以消除我遇到的“超出最大调用堆栈大小”错误。

在前面我称之为:

    verifMail(uid){
    console.log('fetching on uid : ' + uid);
    var func = firebase.functions().httpsCallable('confirmMail');
        console.log(func);
        func({ uid : uid })
    .then((responseValue)=> {
        console.log(responseValue);
        this.setState({ isProcessDone : true});
    })
    .catch((error) => {
        console.error(error);
    });
}

【问题讨论】:

  • 所以在 Firebase 控制台中,您从函数中获取日志 email verified?你试过没有console.log(func)的前端吗?
  • 完全正确,没有日志的结果相同
  • 你在前端初始化了你的firebase项目?而且你的函数也在us-central1中运行?
  • 是的,这一切都完成了,这是我拥有的唯一具有内在承诺的功能,其他功能完美

标签: node.js firebase firebase-realtime-database promise google-cloud-functions


【解决方案1】:

尝试像这样更改您的云功能:

exports.confirmMail = (data) => {
    return admin.database().ref(`/Email-Verifications/${data.uid}`).once('value')
    .then((snapshot) => {
        let uid = snapshot.val().userId;
        return admin.auth().updateUser(uid, { emailVerified : true })
            .then((userRecord)=>{
                return admin.database().ref(`/Email-Verifications/${userRecord.uid}`).set(null)
                .then(()=>{
                    console.log('email verified');
                    return { uid: uid };
                })
                .catch((error)=>{
                    console.log(error);
                    return error;                    
                })
            })
    })
    .catch((error)=>{
        console.log(error);
        return error;
    })
}

您从上一个.then() 返回的信息应包含您希望在前端获取的数据。

但我不确定我是否忘记了什么,因为缺少指出我的语法错误的 IDE。

【讨论】:

  • mhhh ...结果有所不同,但仍然不是那样。在我的控制台中,我得到了一个 data:null,之前我有 data:{},并且该函数再次正确执行(在日志中看到)
  • 谢谢你,运气还是不行
  • 好的,解决了!在最后一个 .then 之前的最后一次编辑中只缺少一个返回。现在它可以完美运行了,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多