Yana Taranets
Yana Taranets

Reputation: 27

Adonis controller doesn't store information to DB

I am writing a page where the user should put some information in two text areas, and this text data should write into DB. The row in DB creates with NULL in these two areas and DateTime is written there.

So in my database I have id|usuario_nombre|file_nombre|created_at| updated_at

and usuario_nombre|file_nombre have NULL, and others have information.

Can you help, please?

Controller :

'use strict'

const Upload = use('App/Models/Upload')


class UploadController {
    async index({ view }) 
    {
        const uploads = await Upload.all();
        return view.render('/', {
            uploads: uploads.toJSON()
        })
    }
    async create({ view }) 
    {
        return view.render('/upload');
    }

    async store({ request, response }) 
    {
        const upload = new Upload();
        upload.usuario_nombre = request.input('usuario_nombre');
        upload.file_nombre = request.input('profilefile');
        upload.save();
        response.redirect('/');
    }
}

module.exports = UploadController

Form with inputs :

<form method="POST" enctype="multipart/form-data">
   {{ csrfField() }}
   <input type="text" id="usuario_nombre"  name="usuario_nombre"/>
   <input type="text" name="profilefile" id="profilefile"/>
   <button type="submit"> Submit </button>
</form>

Routes :

Route.get('/upload/create', 'UploadController.create')
Route.get('/upload', 'UploadController.store')
    
Route.get('/upload/create', 'UploadController.create')
Route.get('/upload', 'UploadController.store')

Migration :

'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

class UploadsSchema extends Schema {
    up() {
        this.create('uploads', (table) => {
            table.increments()
            table.string('usuario_nombre')
            table.string('file_nombre')
            table.timestamps()
        })
    }
    down() {
        this.drop('uploads')
    }
}
module.exports = UploadsSchema

Upvotes: 0

Views: 290

Answers (1)

Use await upload.save(); because save() is a async operation

Upvotes: 1

Related Questions