我已经通过使用“羽毛生成服务”创建了这个服务 - 并选择了 sequalized 我希望能够使用 axios 从客户端调用该服务,目前我的 axios 调用如下所示:
const formData = new FormData()
formData.append('serverPath', 'some path')
await this.$axios.post(`${process.env.BACKEND}/uploadb`, formData, {
timeout: -1,
headers: { Authorization: `Bearer ${that.$store.state.auth.accessToken}` },
cancelToken: this.$options.source.token,
onUploadProgress: (event) => {
that.progress = Math.round((event.loaded * 100) / event.total)
},
onError: (error) => {
systemlog.create({ user: that.$store.state.auth.user == null ? 'unknown' : that.$store.state.auth.user.username, component: 'fileupload', method: 'submin', logtype: 'info', module: 'client', text: 'error:' + error })
}
})
我的服务是这样的
// Initializes the `uploadb` service on path `/uploadb`
const { Uploadb } = require('./uploadb.class');
const createModel = require('../../models/uploadb.model');
const hooks = require('./uploadb.hooks');
const fs = require('fs');
const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const appconfig = feathers().configure(configuration());
const path = require('path');
const destbase = appconfig.get('uploadpath');
module.exports = function (app) {
const options = {
Model: createModel(app),
paginate: app.get('paginate')
};
// Initialize our service with any options it requires
app.use('/uploadb', async (req, res,next) => {
req.pipe(req.busboy); // Pipe it trough busboy
req.busboy.on('file', (fieldname, file, filename) => {
// req.body.serverpath is undefined here
// I have set serverPath in the forData object like this
// formData.append('serverPath', that.newserverpath)
let dest = destbase+'/'+req.body.serverPath;
console.log(`Upload of '${path.join(dest, filename)}' started`);
// the values i set here is stored in the database
req.body.filename=filename;
// Create a write stream of the new file
const fstream = fs.createWriteStream(path.join(dest, filename));
// Pipe it trough
file.pipe(fstream);
// On finish of the upload
fstream.on('close', () => {
console.log(`Upload of '${filename}' finished`);
//res.send(res.data);
next();
});
});
},new Uploadb(options, app));
// Get our initialized service so that we can register hooks
const service = app.service('uploadb');
service.hooks(hooks);
};
几乎可以工作 - 文件已保存 - 并将一行写入 updateb 表 - 但在我使用 multer 找到的另一个示例中 - req.body 已填充 - 我需要做什么才能让服务存储数据我在formdata中附加到数据库?