Облачные функции Firebase - ошибка соединения - PullRequest
0 голосов
/ 23 апреля 2020

Я получаю 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");
  });
});

1 Ответ

1 голос
/ 23 апреля 2020

Следующие изменения должны помочь:

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

        });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...