我正在尝试使用multer处理feathersjs中的多部分表单文件上传,我尝试使用req.picture
和blobService
将blobStorage
中包含的文件复制到服务器并获取该文件的URL,然后用返回的url替换req.picture
。之后req
应该移交给feathers-mongoose
以保留在数据库中。通过官方文档https://docs.feathersjs.com/guides/advanced/file-uploading.html我已经提出了以下内容,但我无处可去。
物品服务|此外,只有当我将blobService({ Model: blobStorage})
更改为createService(options)
时,其他方法(如get)才会生效,否则不会。
// 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);
}
};
项目服务挂钩
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};
}
}
}
这是我对邮递员的反应
{
"name": "GeneralError",
"message": "Cannot read property 'startsWith' of undefined",
"code": 500,
"className": "general-error",
"data": {},
"errors": {}
}