0

I have an error in angular js, when setting application.apikey with response.data.

HTML:

<div class="row">
    <div class="form-group col-xs-6">
        <label>1:</label>

        <input type="text" class="form-control" name="apikey" ng-model="applicationn.apikey"></input>
    </div>

    <div class="form-group col-xs-6">
        <label>2:</label>
        <input type="text" class="form-control" name="apikeysecret" ng-model="application.apisecret"></input>
    </div>
</div>

AngularJS:

$scope.getApiKey = function () {
            $http({
            method: 'GET',
            url: '/applications/generateApiKey'
        }).then(function success(response) {
            console.log(response.data),
            $scope.application.apikey = response.data.apikey;
        });
    }

Console:

    {id: null, name: null, apikey: "cce4866e0b23c721b1e8d733a4cf48c1", apisecret: "914c87f20473c4fac9fbbab65de6e6b0", custom_data: null}
angular.js:14525 TypeError: Cannot set property 'apikey' of undefined
2
  • you probably need to parse the response with JSON.parse(data) Commented Aug 25, 2017 at 10:42
  • 2
    try $scope.application={ apikey : response.data.apikey} Commented Aug 25, 2017 at 10:44

1 Answer 1

1

You have not defined $scope.application and trying to define its property.

Try this :

$scope.application = {};
$scope.getApiKey = function () {
            $http({
            method: 'GET',
            url: '/applications/generateApiKey'
        }).then(function success(response) {
            console.log(response.data),
            $scope.application.apikey = response.data.apikey;
        });
    }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, so if i get it right, everytime i declare variable in html file, i must declare it in controller too?
no its not necessary, the problem was, you used $scope.application.apikey without defining $scope.application, you could have used $scope.apikey without defining anything

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.