Tim Dirks
Tim Dirks

Reputation: 449

Node js file upload issue

I'm trying to upload a file to nanosets API. I uploaded the following node js function to firebase and trying to excess it with following URL for example with a file in body (trying to test this with postman)

Node js function looks like:

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

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

        busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
            const filepath = path.join(os.tmpdir(), filename);
            uploadData = {modelId: '4bc54977-60cf-4415-a417-c39f1c18b83f', file: fs.createReadStream(filename), type: mimetype};
            const options = {
                url: 'https://app.nanonets.com/api/v2/OCR/Model/XXXXXXX-60cf-4415-a417-c39f1c18b83f/LabelFile/',
                formData: uploadData,
                headers: {
                    'Authorization': 'Basic ' + Buffer.from('tiOJNxuDbdl40lXXXXXXXXXXFTYbY' + ':').toString('base64')
                }
            };

            request.post(options, function (err, httpResponse, body) {
                if (err) {
                    console.log(err);
                }
                console.log(body)
            });


        });
        busboy.on("finish", () => {
            res.status(200).json({
                message: "It worked!"
            });

        });
        busboy.end(req.rawBody);

    });
});

Why I check the logs with firebase functions:log I get the following results:

2020-06-06T09:35:06.168774140Z D uploadFile: Function execution started 2020-06-06T09:35:06.344Z I uploadFile: invoice_4.pdf 2020-06-06T09:35:06.432Z I uploadFile: FileStream { 2020-06-06T09:35:06.439Z E uploadFile: TypeError: source.pause is not a function

Anyone an idea? How to pass the file to nanosets?

Upvotes: 1

Views: 121

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317808

In order to make outgoing requests from Cloud Functions, your project must be on the Blaze payment plan.

Upvotes: 2

Related Questions