[Vuejs]-Set initial value of input in vue.js formulate to data from database

0👍

this.row is an empty string before the API request is done, therefore you cannot access this.row["answerq1"]. You need to wait for the API request to finish.

Make this change and it should work:

data() {
  return {
    row: "",
    values: {
      question1: "" // set to empty string
    }
  };
}

0👍

I have found the answer to my question for anyone encountering a similar problem:

new Vue({
    el: '#app',
    created() {
        this.fetchData();
    },
    

    data: {
        row: [],
        values: {
            question1: null
        }
    },

    
    methods: {
        fetchData() {
            axios.get('retrieve.php')
                .then((response) => {
                    this.row = response.data;
                    
                    this.values.question1 = this.row["answerq1"];
            
                });
        },
    }
})

Leave a comment