Firebase云函数 – 连接错误

我得到 Function execution took 5910 ms, finished with status: 'connection error' 每当我调用我的云函数时,都会在日志中出现。我相信这与承诺和返回或 !d.exists 但我一直没能进一步缩小范围。

函数。

exports.useInvite = functions.https.onCall((data, context) => {
  if (context.auth) {
    throw new functions.https.HttpsError('failed-precondition', "cannot be authenticated");
  }

  if (!data.code) {
    throw new functions.https.HttpsError('failed-precondition', "must provide an invite code");
  }

  console.log("Code: ", data.code);

  return admin.firestore().collection("invites").doc(data.code).get().then(d => {
    console.log("Exists: ", d.exists);
    if (!d.exists) {
      throw new functions.https.HttpsError('failed-precondition', "invalid invite code");
    }

    const added = new Date(d.data().created.seconds * 1000);

    const week = 60 * 60 * 24 * 7 * 1000;

    if ((new Date() - added) < week) {
      return admin.firestore().collection("invites").doc(data.code).update({
        validated: true
      }).then(() => {
        return null
      }).catch(() => {
        throw new functions.https.HttpsError('failed-precondition', "invalid invite code");
      });
    } else {
      console.log("Expired!");
      throw new functions.https.HttpsError('failed-precondition', "invite code expired");
    }
  }).catch(e => {
    console.log(e);
    throw new functions.https.HttpsError('failed-precondition', "invalid invite code");
  });
});

解决方案:

下面的修改应该可以解决这个问题。

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

    class ExpiredCodeError extends Error {
        constructor(message) {
            super(message);
            this.message = message;
            this.type = 'ExpiredCodeError';
        }
    }

    class InvalidCodeError extends Error {
        constructor(message) {
            super(message);
            this.message = message;
            this.type = 'InvalidCodeError';
        }
    }

    if (context.auth) {
        throw new functions.https.HttpsError('failed-precondition', "cannot be authenticated");
    }

    if (!data.code) {
        throw new functions.https.HttpsError('failed-precondition', "must provide an invite code");
    }

    console.log("Code: ", data.code);

    return admin.firestore().collection("invites").doc(data.code).get()
        .then(d => {
            console.log("Exists: ", d.exists);
            if (!d.exists) {
                throw new InvalidCodeError("invalid invite code");
            }

            const added = new Date(d.data().created.seconds * 1000);

            const week = 60 * 60 * 24 * 7 * 1000;

            if ((new Date() - added) < week) {
                return admin.firestore().collection("invites").doc(data.code).update({
                    validated: true
                })
            } else {
                console.log("Expired!");
                throw new ExpiredCodeError("invite code expired");
            }
        })
        .then(() => {
            return { status: "OK" }
        })
        .catch(e => {
            console.log(e);

            if (e.type === 'ExpiredCodeError') {
                throw new functions.https.HttpsError('precondition', e.message);
            } else if (e.type === 'InvalidCodeError') {
                //May be merged with the above clause...
                throw new functions.https.HttpsError('precondition', e.message);
            } else {
                throw new functions.https.HttpsError('internal', e.message);
            }

        });
});

给TA打赏
共{{data.count}}人
人已打赏
解决方案

如何使用内部区域?

2022-5-11 10:00:17

解决方案

如何在保存过程中,如果输入字段不是数字,如何显示验证信息?

2022-5-11 11:00:06

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索