feathersjs-ecosystem / feathers-blob

Feathers service for blob storage, like S3.
http://feathersjs.com
MIT License
92 stars 32 forks source link

Route.post() requires a callback function but got a Object #86

Closed apmcodes closed 3 years ago

apmcodes commented 3 years ago

If we use blobservice in app.post() we get the below error, but not with app.use()

app/node_modules/express/lib/router/route.js:202
        throw new Error(msg);
        ^

Error: Route.post() requires a callback function but got a [object Object]
    at Route.<computed> [as post] (app/node_modules/express/lib/router/route.js:202:15)
    at Function.app.<computed> [as post] (app/node_modules/express/lib/application.js:482:19)
    at Function.module.exports (app/src/services/bulk/bulk.service.js:44:9)
    at Function.configure (app/node_modules/@feathersjs/feathers/lib/application.js:59:8)
    at Function.module.exports (app/src/services/index.js:24:9)
    at Function.configure (app/node_modules/@feathersjs/feathers/lib/application.js:59:8)
    at Object.<anonymous> (app/src/app.js:62:5)

Code

app.post('/upload',
        uploader,
        // another middleware, to transfer the received file to feathers
        // multer generates file for uploaded file and body for form data
        function (req, res, next) {
            req.feathers.file = req.file;   // for upload file method file
            req.feathers.body = req.body;   // for paste method data emails
            next();
        },
        BlobService({ Model: blobStorage }),
        },
    );

NodeJS version: v12.22.1

    "@feathersjs/authentication-local": "^4.5.11",
    "@feathersjs/authentication-oauth": "^4.5.11",
    "@feathersjs/configuration": "^4.5.11",
    "@feathersjs/errors": "^4.5.11",
    "@feathersjs/express": "^4.5.11",
    "@feathersjs/feathers": "^4.5.11",
    "@feathersjs/socketio": "^4.5.11",
    "@feathersjs/transport-commons": "^4.5.11",
    "feathers-blob": "^2.5.0",
    "s3-blob-store": "^4.1.1",
daffl commented 3 years ago

Services can only be used with app.use not method specific Express handlers.

app.use('/upload',
        uploader,
        // another middleware, to transfer the received file to feathers
        // multer generates file for uploaded file and body for form data
        function (req, res, next) {
            req.feathers.file = req.file;   // for upload file method file
            req.feathers.body = req.body;   // for paste method data emails
            next();
        },
        BlobService({ Model: blobStorage }),
        },
    );
apmcodes commented 3 years ago

Thank you @daffl.

With the below code, I can use custom service as well as seperate middleware for POST. Using blobService.create() method inside another middleware.

this.blobService = BlobService(blobServiceOptions);

upload.service.js

    // setup blobService
    ...
    blobService = BlobService(blobServiceOptions);

    app.post('/upload',
        uploader,
        // another middleware, to transfer the received file to feathers
        // multer generates file for uploaded file and body for form data
        function (req, res, next) {
            req.feathers.file = req.file;   // for upload file method file
            next();
        }
        async function (req, res, next) {
            // setup blobBody = {id, buffer: req.feathers.file.buffer}
            ...
            const response = await blobService.create(blobBody)
            ...
            next();
        }
    );
    app.use('/v1/bulk', new Bulk(options, app));