Cannot Update Data


Hello, this is my first time using node and i choose adonisjs because similarity to laravel and its very great.

I have problems that i cannot update my data. Here my code:

‘use strict’

const Model = use(‘Model’)

class Setting extends Model {

static getAll() {
    return this.all()

static updateBySetting(setting, values, additional = false) {
    // this.only = ['values','additional']
    let exec = this.query().where('setting',setting).update({values: values, additional: additional})
    return exec


module.exports = Setting

Here my controller:

‘use strict’

const Setting = use(‘App/Models/Setting’)
const each = use(‘foreach’)
const Utils = use(‘App/Libraries/Utils’)

class SettingController {
async getIndex({view}) {
let rs_settings = await Setting.getAll()
let rs_key_by_setting = new Object()

    each(rs_settings.toJSON(), function (value) {
        rs_key_by_setting[value.setting] = value

    return view.render('backend.settings', {settings: rs_key_by_setting})

async postUpdate({request}) {
    let data = request.all()

    try {
        await each(data, function (value, key) {
            let values = typeof value.values == 'string' ? value.values : null
            let additional = typeof value.additional == 'string' ? value.additional : null

            return Setting.updateBySetting(key, values, additional)

        return Utils.notification(200, 'Sukses menyimpan data.')
    } catch (e) {
        return Utils.notification(403, 'Gagal menyimpan data')


module.exports = SettingController

BTW my background is Laravel, sorry if this question is n00b :slight_smile:

Thank you so much, adonisjs is awesome !



Database operations are async and return a promise. You need to use async/await syntax or wait for promise to be resolved.


Try something like this:

const promises = Object
.map((key) => {
  // ...
  return Setting.updateBySetting(key, values, additional)

await Promise.all(promises)

return Utils.notification(200, 'Sukses menyimpan data.')

You should probably use a reducer though, depending on how many key/value pairs you have. map would execute all of the queries async, not waiting for the previous query to finish. And you can easily hit db max connections.


It works, thank you so much @moltar. Now i understant the Promise concept :blush: