I am trying to to handle a multipart form file upload in feathersjs using multer, I have tried to copy the file contained in req.picture
to the server using blobService
and blobStorage
and get a url to the file and then replace the req.picture
with the returned url. After that req
should be handed over to feathers-mongoose
to persist in the database. Going through official docs https://docs.feathersjs.com/guides/advanced/file-uploading.html I have come up with following but I am getting nowhere with it.
Items Service | Also Only if I change blobService({ Model: blobStorage})
to createService(options)
then other methods like get works , if not it does not.
// Initializes the `items` service on path `/items`
const createService = require('feathers-mongoose');
const createModel = require('../../models/items.model');
const hooks = require('./items.hooks');
const filters = require('./items.filters');
const multer = require('multer');
const multipartMiddleware = multer();
const blobService = require("feathers-blob");
const fs = require("fs-blob-store");
const blobStorage = fs("./public/items/pictures");
module.exports = function () {
const app = this;
const Model = createModel(app);
const paginate = app.get('paginate');
const options = {
name: 'items',
Model,
paginate
};
// Initialize our service with any options it requires
app.use('/items',
multipartMiddleware.single('picture'),
function(req, res, next){
req.feathers.file = req.picture;
req.picture = 'url to the file';
next();
},
blobService({ Model: blobStorage}));
// Get our initialized service so that we can register hooks and filters
const service = app.service('items');
service.hooks(hooks);
if (service.filter) {
service.filter(filters);
}
};
Items service hooks
const { authenticate } = require('feathers-authentication').hooks;
const fileUpload = require('../../hooks/file-upload').hooks;
module.exports = {
before: {
all: [ authenticate('jwt') ],
find: [],
get: [],
create: [fileUpload],
update: [fileUpload],
patch: [],
remove: []
},
Hook for file upload (file-upload.js)
const dauria = require('dauria');
module.exports = {
hooks: function(hook) {
if (!hook.data.uri && hook.params.file){
const file = hook.params.file;
const uri = dauria.getBase64DataURI(file.buffer, file.mimetype);
hook.data = {uri: uri};
}
}
}
This is the response I get on postman
{
"name": "GeneralError",
"message": "Cannot read property 'startsWith' of undefined",
"code": 500,
"className": "general-error",
"data": {},
"errors": {}
}