Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

getting data from databse

ok so I have content.json and I also have a similar structure in a database now, great!

How can I set my variables to get the data from the api?

getting data from the file works fine

import Content from '@/Content.json'
// read in the english version and parse it
 const myObjStr = JSON.stringify(Content.en);
const content = JSON.parse(myObjStr);

getting data from API works but don't I have to set the variable content to res.data.en somehow?

import axios from 'axios'
const content = axios.get('http://127.0.0.1:8000/api/courses').then((res) => {
  console.log('DATABASE:', res.data);
});
0 likes
5 replies
tykus's avatar

Should be enough to do this:

let content;

import axios from 'axios'
axios.get('http://127.0.0.1:8000/api/courses').then(({data}) => content = data)
1 like
boyjarv's avatar

thanks but I can't access variable content outside of the axios call

let content;

axios.get('http://127.0.0.1:8000/api/courses').then((data) => {
  content = data.data[0].en
  console.log('content inside axios', content);
});
console.log('content outside axios', content);

console:

content outside axios undefined
content inside axios {_dev: false, _edit: false, _css: false, label: 'English', config: {…}, …}
1 like
MaverickChan's avatar

@boyjarv

import axios from 'axios'
import { ref } from 'vue'
const content = ref()
const getData = async ()=> {
	try {
			const response= await axios.get('http://127.0.0.1:8000/api/courses')
			content.value = response.data
			console.log(content.value)
		} catch(error) {
			console.log(error.response.data)
	}
}
getData() //call the method to get data
1 like
boyjarv's avatar

@MaverickChan

thanks, that kind of works:

const content = ref()
const allContent = ref()
const getData = async ()=> {
	try {
			const response= await axios.get('http://127.0.0.1:8000/api/courses')
			content.value = response.data[0].en
      allContent.value = response.data[0]
			
		} catch(error) {
			console.log(error.response.data)
    } 
	}

getData()
Vue.use(Vuex)
console.log('content: ', content)
console.log('content value: ', content.value)
export default new Vuex.Store({
  state: {
    bookmark: '',
    courseContent: content.value,

where I am console logging out: console.log('content value: ', content.value) I get: undefined

Where I am console logging: console.log('content: ', content), I get:

content:  
{__v_isRef: true, __v_isShallow: false, dep: Dep}
value
: 
Object
config
: 
(…)
global
: 
boyjarv's avatar

can anyone help? I cant get coontent.value outputting to screen, it shows in console?!

Please or to participate in this conversation.