【问题标题】:Firebase Function returns null data objectFirebase 函数返回空数据对象
【发布时间】:2019-05-29 14:52:23
【问题描述】:

我有一个有效的 Firebase 云函数(它以管理员身份写入我的数据库,并且还检查具有电子邮件字符串 ID 的现有条目)。我的问题是它没有正确地将数据返回到客户端应用程序。

当我跟踪result 的值时,它是{data: null}

如果电子邮件存在,我也想抛出一个错误,但它抱怨未处理的承诺(请参阅下面代码中的 cmets)。

所以这里有两个问题。第一个是最紧迫的。

exports.saveData = functions.https.onCall( (data, context) => {

    // check if the email exists
    admin.database().ref('competition_entries/' + data.compId + '/' + data.emailStringId).once('value').then(function(snapshot) {
        if (snapshot.val() == null){

          console.log("Writing data to "+data.compId+" : "+data.emailStringId);

          let current_datetime = new Date()
          let formatted_time = current_datetime.getHours() + ":" + current_datetime.getMinutes() + ":" + current_datetime.getSeconds();
          let formatted_date = current_datetime.getDate() + "/" + (current_datetime.getMonth() + 1) + "/" + current_datetime.getFullYear();
          let timestamp_string = formatted_time+" "+formatted_date;
          console.log(formatted_date)

          admin.database().ref('competition_entries/' + data.compId + '/' + data.emailStringId).set({
            name: data.name,
            email: data.email,
            phone: data.phone,
            selectedItem: data.selectedItem,
            submitted_date: timestamp_string
          })
          .then(() => {
            console.log("success");
            return {
              "error": "none",
              "message": "success"
            };
          }).catch(function(error) {
            throw error;
          });

        }else{
            // email address exists as a data entry so tell the user that they have already submitted
            let code = "already-exists";
            let message = "Email address already exists";
            let details = "A user with the same email address has already entered the competition.";
            console.log(message+"/"+details);

            /*
              TODO: throwing an HttpsError directly here was causing an unresolved promise error. This would be the better way to tell the client that the user has already submitted their data.
            */
            // throw error;
            //throw new functions.https.HttpsError(code, message, details);

            // this resolves the promise error but is a hack
            return {
              error: new functions.https.HttpsError(code, message, details),
              message: message
            }
        }
    }).catch(function(error) {
      throw error;
    });
});

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    我认为问题是你没有返回足够的。函数需要每个函数返回一些东西才能成功执行。

    通常是return truereturn Promise

    所以应该是这样的:

    //check if email exist
    return admin.databse...
    
        //If there is no user create it
         return.admin...
    

    代码确实写入了数据库,但由于它没有异步发生,因此代码将继续从上到下执行,从而使函数对客户端的响应变得混乱。

    你应该

    • if (!snapshot) 短路更好,因为快照可能未定义,如果快照数据为空,则在检查时会导致异常
    • 您不需要为数据库使用 admin sdk,Functions 被认为是受信任的环境,因此操作以管理员权限运行。尽管有规则或用户限制,你可以 CRUD 任何东西

    【讨论】:

    • 我刚刚发现了有关添加“返回”的一些信息,但也感谢其他提示!欣赏它。
    • 我现在的问题是我部署了它并且遇到了 CORS 问题。有什么建议吗?
    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    相关资源
    最近更新 更多