Firebase函数RangeError:超出最大调用堆栈大小

时间:2018-12-03 12:20:24

标签: function typescript firebase google-cloud-firestore

我有一个Callable函数,可以上传图像并相应地更新Firestore和Storage。该函数执行其应做的工作。但我仍然收到此错误:

  

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

这是函数:

    export const uploadImageToStripe = functions.https.onCall(async (data, context) => {
    let businessDoc: DocumentSnapshot
    try {
        if (!fireStoreDB) {
            fireStoreDB = admin.firestore();
            fireStoreDB.settings(settings);
        }
        businessDoc = await fireStoreDB.collection('businesses').doc(data.business_id).get()
        const bucketName = functions.config().storage.default_bucket;
        const tempLocalFile = path.join(os.tmpdir(), 'img.jpg').trim();
        const tempLocalDir = path.dirname(tempLocalFile);
        const bucket = admin.storage().bucket(bucketName);

        // Create the temp directory where the storage file will be downloaded.
        await mkdirp(tempLocalDir);
        console.log('Temporary directory has been created', tempLocalDir);
        // Download file from bucket.
        await bucket.file(data.photo_location).download({ destination: tempLocalFile });
        console.log('The file has been downloaded to', tempLocalFile);

        // Downloads the file
        console.log(`gs://${bucketName}/${data.photo_location} downloaded to ${tempLocalDir}.`)
        const uploadedFile: stripeM.fileUploads.IFileUpdate = await stripe.fileUploads.create({
            file: {
                data: fs.readFileSync(tempLocalFile),
                name: 'img.jpg',
                type: 'application.octet-stream',
            }
        });
        if (!businessDoc.exists) {
            throw new functions.https.HttpsError('not-found', `Couldn't find business document ` + data.business_id);
        }
        await stripe.accounts.update(businessDoc.data().stripeId,
                { document: uploadedFile.id });
        await businessDoc.ref.update({ "photoNeeded": false })
        return await bucket.file(data.photo_location).delete()
    } catch (error) {
        console.error(error);
        await businessDoc.ref.update({ "photoNeeded": true })
        throw new functions.https.HttpsError('unavailable', `failed to upload photo to stripe`);
    }
})

有什么主意我为什么会出现此错误?

1 个答案:

答案 0 :(得分:0)

此行抛出错误:

return await bucket.file(data.photo_location).delete()

将其拆分为:

await bucket.file(data.photo_location).delete()
return "Success" 

解决它。