adonuxt-template icon indicating copy to clipboard operation
adonuxt-template copied to clipboard

How to access the database

Open rwatts3 opened this issue 7 years ago • 1 comments

Greetings,

What is the best way to access the database? I believe using vuex store would be the best way to go but i'm not sure how to access the database within the store.

This question is available on Nuxt.js community (#c53)

rwatts3 avatar Aug 17 '17 03:08 rwatts3

I would use adonis to do stuff with database.

Then create some api routes that vue/nuxt will call for data. And then you can play with your data. I think that is the only (at least right) way to do it.

http://adonisjs.com/docs/3.2/adonis-blog-part3

for an example a way to get Locale

app\Controllers\Http\LangController.js

'use strict'

const Antl = use('Antl')

class LangController {
  async get ({ response }) {
    response.json({
      lang: Antl.getLocale()
    })
  }
}

module.exports = LangController

start/routes.js

const Route = use('Route')
Route.get('lang', 'LangController.get')
Route.any('*', 'NuxtController.render')

Then in vue:

<script>
import axios from '~/plugins/axios'

export default {
  methods: {
    changeLanguage (lang) {
      axios().get('/lang').then((response) => {
        console.log(response)
      }).catch((error) => {
        console.log(error)
      })
    }
  }
}
</script>

alanaasmaa avatar Nov 07 '17 17:11 alanaasmaa