Search code examples
vue.jssurveyjs

Get surveyjs questions from db


I am using surveyjs in vue, and I am trying to get my var json questions from my API.

Using vue-resource, I make a GET request to the API, and save the response in questions and put it on var json

export default {
    components: {
           Survey
    },
    data(){
           this.$http.get('api')
           .then(res => res.json())
           .then(questions => {
               this.questions = questions;
           });
           var json = this.questions;
           var model = new SurveyVue.Model(json);
           return {
               survey: model,
               questions:''
           }
    }
}

Using console.log(json), I get undefined. So, what´s the correct way to access API response in this case?


Solution

  • I think you could use something like this :

    export default {
        components: {
               Survey
        },
        data() {
            return {
                survey: {},
                questions: ''
            }
        },
        mounted() {
            let self = this;
            this.$http.get('api')
               .then(questions => {
                   self.questions = questions;
               });
            this.survey = new SurveyVue.Model(this.questions);
        }
    }
    

    You can learn more about the mounted method here. You now should be able to access your survey data.