javascript - Cannot assign info from request properly Angular JS -
i need send few requests server , info write in array. function looks like:
$scope.addinstrument = function(themat) { $scope.data = '{"func":"addinstrument", "thematerial":"' + themat + '", '; $scope.data += '"lifespan":"' + $scope.lifespan + '"}'; server.fetch($scope.data, $rootscope.token).then(function(data) { return data[0].addinstrument; }) }
code in controller calls function:
for (var = 0; < $scope.amount; ++) { $scope.inventnum[$scope.inventnum.length] = $scope.addinstrument(themat); } angular.foreach($scope.inventnum, function(value) { alert(value); })
function sends requests server:
app.service('server', function (authservice) { this.fetch = function (data, token) { return fetch('http://104.197.58.108:8080', { method: 'post', headers: { 'accept': 'application/json', 'content-type': 'application/json' }, body: json.stringify({ data: data, token: token }) }) .then(function(response) { return response.json() }).then(function(data) { if (data.success == false) { return authservice.logout(); } else { return data; } }) } })
after that, alerts 'undefined' right amount of times. tried assign values array in function addinstrument():
$scope.addinstrument = function(themat) { $scope.data = '{"func":"addinstrument", "thematerial":"' + themat + '", '; $scope.data += '"lifespan":"' + $scope.lifespan + '"}'; server.fetch($scope.data, $rootscope.token).then(function(data) { $scope.inventnum[$scope.inventnum.length] = data[0].addinstrument; }) }
but in case there mistakes cannot read property length of undefined.
Comments
Post a Comment