TypeError: Cannot read property 'first_name' of undefined

1,512 views
Skip to first unread message

Andrej Batarilo

unread,
May 26, 2018, 4:10:13 AM5/26/18
to Angular and AngularJS discussion

Entire Error:
AppComponent.html:26 ERROR TypeError: Cannot read property 'first_name' of undefined
    at Object.eval [as updateRenderer] (AppComponent.html:26)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:11940)
    at checkAndUpdateView (core.js:11312)
    at callViewAction (core.js:11548)
    at execComponentViewsAction (core.js:11490)
    at checkAndUpdateView (core.js:11313)
    at callWithDebugContext (core.js:12204)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (core.js:11882)
    at ViewRef_.push../node_modules/@angular/core/fesm5/core.js.ViewRef_.detectChanges (core.js:9692)
    at core.js:5086


Hello im trying to read from a sql database using php files and am sure that I have typed all the sql info correctly,
here is my html:

But apparently it can't read from data, what do I do?

Thank you so much



<html>
<head>
<title>Webslesson Tutorial | AngularJS Tutorial with PHP - Fetch / Select Data from Mysql Database</title>
</head>
<body>
<br /><br />
<div class="container" style="width:500px;">
<h3 align="center">AngularJS Tutorial with PHP - Fetch / Select Data from Mysql Database</h3>
<div ng-app="myapp" ng-controller="usercontroller" ng-init="displayData()">
<label>First Name</label>
<input type="text" name="first_name" ng-model="firstname" class="form-control" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" ng-model="lastname" class="form-control" />
<br />
<input type="submit" name="btnInsert" class="btn btn-info" ng-click="insertData()" value="ADD"/>
<br /><br />
<table class="table table-bordered">
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="x in names">
<td>{{x.first_name}}</td>
<td>{{x.last_name}}</td>
</tr>
</table>
</div>
</div>
</body>
</html>
<script>
var app = angular.module("myapp",[]);
app.controller("usercontroller", function($scope, $http){
$scope.insertData = function(){
$http.post(
"insert.php",
{'firstname':$scope.firstname, 'lastname':$scope.lastname}
).success(function(data){
alert(data);
$scope.firstname = null;
$scope.lastname = null;
$scope.displayData();
});
}
$scope.displayData = function(){
$http.get("select.php")
.success(function(data){
$scope.names = data;
console.log(data);
});
}
});

</script>

Tito

unread,
May 27, 2018, 3:52:55 PM5/27/18
to Angular and AngularJS discussion
what does console.log(data) show?
also is it firstname or is it first_name
Reply all
Reply to author
Forward
0 new messages