Firebase功能:未处理的错误RangeError:超出最大调用堆栈大小

Seb*_*ien 5 firebase typescript google-cloud-functions

我有以下可调用函数,该函数从数据库中获取一些数据,然后使用html-pdf使用该数据创建PDF,将该PDF上载到存储,最后返回存储中文件的名称。它在https形式下工作良好,但我想将其转换为可调用的函数,由于某种原因我无法弄清楚,它因以下错误而崩溃:RangeError:超出了最大调用堆栈大小。

我怀疑这与以下事实有关:html-pdf不适用于promises,而是使用错误/数据回调。但是我试图将其转化为没有成功的承诺。

export const createPdf = functions.https.onCall((data, context) => {
    const refId = data.refId;
    const companyId = data.companyId;
    const userId = context.auth.uid;

    return admin.database().ref('/references').child(companyId).child(refId).once('value', (snapshot) => {
        const filePath = '/references/' + refId + '/pdfs/' + refId + '.pdf';
        const localeId = snapshot.child('locale').val();

        return admin.database().ref('/tags').child(localeId).once('value', (tagsSnapshot) => {
            const jsLocaleId = localeId.replace(/_/, "-");
            const projectDate = moment().locale(jsLocaleId)
                .year(snapshot.child('year').val())
                .month(snapshot.child('month').val() - 1)
                .date(15)
                .format('MMMM YYYY');

            const tags = tagsSnapshot.val();
            const projectCategories = ...
            const pictures = snapshot.child('pictures').val();

            const pdfData = {
                projectName: snapshot.child('projectName').val(),
                surface: snapshot.child('surface').val(),
                companyName: snapshot.child('companyName').val(),
                date: projectDate,
                newBuilding: snapshot.child('newBuilding').val(),
                customerName: snapshot.child('customerName').val(),
                categories: projectCategories,
                address: snapshot.child('address').val().replace(/\n/g, '<br>'),
                satellite: snapshot.child('satellite').val(),
                pictures: !isNullOrUndefined(pictures) ? pictures.map((item) => {
                    return {url: item}
                }) : []
            };
            console.log("data", pdfData);
            const options = {...};

            const localTemplate = path.join(os.tmpdir(), 'share.html');
            const localPDFFile = path.join(os.tmpdir(), 'share.pdf');
            const languageCode = localeId.split("_")[0];

            return admin.storage().bucket().file('/templates/share-' + languageCode + '.html').download({destination: localTemplate}).then(() => {
                const source = fs.readFileSync(localTemplate, 'utf8');
                const html = handlebars.compile(source)(pdfData);
                pdf.create(html, options).toFile(localPDFFile, function (err, result) {
                    if (err) {
                        console.log(err);
                        throw new functions.https.HttpsError('internal', err.message);
                    }

                    return admin.storage().bucket().upload(localPDFFile, {
                        destination: filePath,
                        resumable: false,
                        metadata: {contentType: 'application/pdf'}
                    }).then((files) => {
                        console.log("files", files);
                        return files[0].getMetadata().then((metadata) => {
                            const name = metadata[0]["name"];
                            return {
                                name: name
                            };
                        });
                    }).catch(error => {
                        console.error(error);
                        throw new functions.https.HttpsError('internal', "Could not upload PDF because " + error.message);
                    });
                });
            }).catch((error) => {
                console.error("Could not download template");
                throw new functions.https.HttpsError('internal', error.message);
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 9

可调用函数不应仅返回任何承诺。他们应返回一个承诺,该承诺将与响应一起解决并发送给客户端。您正在返回一个承诺,该承诺将在数据库操作完成时解决。Cloud Functions可能正在尝试序列化promise(一个DataSnapshot对象)中包含的值。这可能包含循环引用,这会在序列化过程中引起问题。

看来您假设返回一个嵌套了三个承诺的承诺,将把响应发送给客户端,但这不是承诺工作的方式。您可以在HTTP函数中解决此问题,因为您可以调用深度嵌套的response.send(),但这在这里不起作用。您将必须取消嵌套所有诺言并依次运行它们。(您现在正在做的事情被视为兑现承诺的坏作风。)

  • @regretoverflow 我在使用 twilio api 时遇到了同样的问题,这对我有用:https://pastebin.com/bWYGFFcC - 基本上我只是返回一个新的承诺来解决我明确需要的任何数据 (2认同)
  • 您可以添加示例承诺代码吗?我正在开始使用 firebase 可调用函数和承诺。 (2认同)