【问题标题】:i am trying to deploy my firebase function below written js?我正在尝试在编写的 js 下部署我的 firebase 功能?
【发布时间】:2020-10-25 03:48:25
【问题描述】:
const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
const Busboy = require('busboy');
const os = require('os');
const path = require('path');
const fs = require('fs');
const fbAdmin = require('firebase-admin');
const uuid = require('uuid/v4');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
const gcconfig = {
    ' '
};

const gcs = require('@google-cloud/storage')(gcconfig);

fbAdmin.initializeApp({ credential: fbAdmin.credential.cert(require('')) });

exports.storeImage = functions.https.onRequest((req, res) => {
    return cors(req, res, () => {
        if (req.method !== 'POST') {
            return res.status(500).json({ message: 'Not allowed.' });
        }

        if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
            return res.status(401).json({ error: 'Unauthorized.' });
        }

        let idToken;
        idToken = req.headers.authorization.split('Bearer ')[1];

        const busboy = new Busboy({ headers: req.headers });
        let uploadData;
        let oldImagePath;

        busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
            const filePath = path.join(os.tmpdir(), filename);
            uploadData = { filePath: filePath, type: mimetype, name: filename };
            file.pipe(fs.createWriteStream(filePath));
        });

        busboy.on('field', (fieldname, value) => {
            oldImagePath = decodeURIComponent(path);
        });

        busboy.on('finish', () => {
            const bucket = gcs.bucket(' ');
            const id = uuid();
            let imagePath = 'images/' + id + '-' + uploadData.name
            if (oldImagePath) {
                imagePath = oldImagePath;
            }

            return fbAdmin.auth().verifyIdToken(idToken).then(decodedToken => {
                return bucket.upload(uploadData.filePath, {
                    uploadType: 'media',
                    destination: imagePath,
                    metadata: {
                        metadata: {
                            contentType: uploadData.type,
                            firebaseStorageDownloadToken: id
                        }
                    }
                });
            }).then(() => {
                return res.status(201).json({
                    imageUrl: 'https://firebasestorage.googleapis.com/v0/b/' + bucket.name + '/o/' + encodeURIComponent(imagePath) + '?alt=media&token=' + id,
                    imagePath: imagePath
                });
            }).catch(error => {
                return res.status(401).json({ error: 'Unauthorized!' });
            });
        });
        return busboy.end(req.rawBody);
    });
});

我正在尝试部署此功能,但我收到此错误(解析您的功能触发器时发生错误。

错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]:包子路径 './v4' 未由 C:\Users\ahmed aziz\AndroidStudioProjects\salers_demo\functions\node_modules\uuid\package.json 中的“导出”定义。所以请帮帮我

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions


    【解决方案1】:

    错误是抱怨您使用require('uuid/v4')

    API documentation for uuid 建议你像这样导入和使用它:

    import { v4 as uuidv4 } from 'uuid';
    uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
    

    【讨论】:

    • 所以我应该删除我的|需要 ('uuid/v4')|并写|改为导入...等??
    • 看我对js一无所知?
    猜你喜欢
    • 2021-05-15
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    相关资源
    最近更新 更多