1

I am trying to get the response data from my REST API and to display it in my vue.js application as follows:

var database = new Vue({
    el: '#database',
    data: {
        databaseConfiguration: {
            type: '',
            url: '',
            port: '',
            username: '',
            password: ''
        },
        errors: []
    },
    created: function () {
        axios.get('/config/database')
            .then(function (response) {
                this.databaseConfiguration = response.data;
                console.log(response.data);
            })
            .catch(function (error) {
                this.errors.push(error);
                console.log(error);
            })
    }
}
)

If I debug it, I see that the response is correctly fetched from the REST API. But unfortunately the data is not displayed on the html page. The html code looks as follows:

<div id="database" class="panel panel-default panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Database Configuration</h3>
    </div>
    <div class="panel-body">
        <form class="form-horizontal">
            <div class="form-group">
                <label for="inputType" class="col-sm-2 control-label">Type</label>
                <div class="col-sm-10">
                    <input type="text" v-model="databaseConfiguration.type" class="form-control" id="inputType"
                           placeholder="Database type">
                </div>
            </div>
            <div class="form-group">
                <label for="inputUrl" class="col-sm-2 control-label">Url</label>
                <div class="col-sm-10">
                    <input type="text" v-model="databaseConfiguration.url" class="form-control" id="inputUrl"
                           placeholder="Url">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPort" class="col-sm-2 control-label">Port</label>
                <div class="col-sm-10">
                    <input type="number" v-model="databaseConfiguration.port" class="form-control" id="inputPort"
                           placeholder="Port">
                </div>
            </div>
            <div class="form-group">
                <label for="inputUsername" class="col-sm-2 control-label">Username</label>
                <div class="col-sm-10">
                    <input type="text" v-model="databaseConfiguration.username" class="form-control"
                           id="inputUsername" placeholder="Password">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword" class="col-sm-2 control-label">Password</label>
                <div class="col-sm-10">
                    <input type="password" v-model="databaseConfiguration.password" class="form-control"
                           id="inputPassword" placeholder="Password">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">Save</button>
                </div>
            </div>
        </form>
    </div>
</div>

All the code can be found here.

Bert
  • 80,741
  • 17
  • 199
  • 164
René Winkler
  • 6,508
  • 7
  • 42
  • 69

1 Answers1

4

Your this is pointing to the wrong object in your callbacks. Try using an arrow function, a closure, or bind.

Here is an example using an arrow function.

axios.get('/config/database')
        .then(response => {
            this.databaseConfiguration = response.data;
            console.log(response.data);
        })
        .catch(error =>{
            this.errors.push(error);
            console.log(error);
        })

See How to access the correct this inside a callback.

Bert
  • 80,741
  • 17
  • 199
  • 164