javascript - Firebase 函数返回空数据对象

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

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

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

如果电子邮件存在,我也想抛出一个错误,但它提示未处理的 promise (请参阅下面代码中的注释)。

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

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;
    });
});

最佳答案

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

通常是返回true返回Promise

所以它应该是这样的:

//check if email exist
return admin.databse...

    //If there is no user create it
     return.admin...

代码确实写入了数据库,但由于异步发生没有返回,并且代码将继续从上到下执行,使得函数向客户端响应乱码。

你应该

  • if (!snapshot) 短路更好,因为如果快照数据为空,则快照可能未定义,从而在 checkinf 时导致异常
  • 您不需要使用数据库的管理 SDK,Functions 被视为受信任的环境,因此操作是使用管理员权限运行的。无论规则或用户限制如何,您都可以对任何内容进行 CRUD

关于javascript - Firebase 函数返回空数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363393/

相关文章:

javascript - 在哪里可以找到 neo4j 3.0 驱动程序 session .run .then 等方法的列表

ios - 如何使用依赖的 Firebase 创建 Pod 框架

Firebase 简单登录 - 防止新用户

Javascript 在 mailto 正文中添加换行符

javascript - 在使用模式插入输入之前自动格式化值

ios - 搜索子值 Firebase/Swift

android - 错误:(66, 30)错误:PhoneAuthProvider包不存在

ios - 如何从一个 Xcode 项目访问多个 Firebase 数据库

Javascript 动态移动 IMG 元素

android - 从 Google Analytics 到 Firebase Analytics 的行为流