无法获取请求,在adonisjs中工作的file()

时间:2019-02-12 23:00:15

标签: adonis.js

当我尝试同时调用文本和文件的请求时,没有得到响应。邮递员只显示200,而没有任何内容更新到数据库。

'use strict';

const Pendingupload = use('App/Models/Pendingupload');
const Helpers = use('Helpers');

class UploadController {
  async create({ request, response, auth, session }) {
    const { title, description } = request.all();

    const validationOptions = {
      types: ['image'],
      size: '2mb',
      extnames: ['png', 'gif']
    };
    // when call it; there is an error
    const avatar = request.file('file', validationOptions);

    try {
      const Pendinguploads = new Pendingupload();

      Pendinguploads.title = title;
      Pendinguploads.description = description;
      Pendinguploads.user_id = await auth.user.id;
      Pendinguploads.image_url = '';

      // this is when validation occurs

      Pendinguploads.image_url = new Date().getTime + '.' + avatar.subtype;

      await avatar.move(Helpers.tmpPath('pendinguploads/pic'));
      if (!avatar.moved()) {
        return avatar.errors();
      }

      console.log(Pendinguploads.title);
      console.log(Pendinguploads.description);

      // })

      //await request.multipart.process();
      await Pendinguploads.save();

      session.flash({ message: 'Your video has been posted!.' });
      return response.redirect('upload');
    } catch (error) {
      session.flash({ uploadError: 'could not upload file' + avatar });
      return response.redirect('back');
    }
  }
}

module.exports = UploadController;

0 个答案:

没有答案