Thursday 20 April 2017

json data not being recognised as rows (angular.js)

I'm trying to get a basic angular.js page to display information retrieved with a http get request from a node.js server. however I'm running into issues where angular is not recognizing the json I'm getting properly or something along these lines. Below is the index.html

<!DOCTYPE html>
<html >
<script 
  src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl">
<h1></h1>
<table>
<tr ng-repeat="x in names">
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://127.0.0.1:8081/process_get")
.then(function (response) {$scope.names = response.data;});
});
</script>

and then here is the relevant snippets from the node.js app.js file

var queryString = 'SELECT * FROM posts';
var retRows;
connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
    console.log(rows);
    retRows = rows;
});
app.get('/process_get', function (req, res) {
// Prepare output in JSON format
console.log(response);
res.end(JSON.stringify(retRows));
})

finally the json data is being displayed as follows which i think should be fine?

{"retRows":[{"id":1,"title":"global work and travel co.","pBody":"One of the 
worlds leading and largest youth travel 
brands","category":"Travel","url":"https://globalworkandtravel.com"},
{"id":2,"title":"travel.com.au","pBody":"One destination endless 
possibilities","category":"Travel","url":"https://www.travel.com.au"},
{"id":3,"title":"Flight Centre","pBody":"Australias leading travel 
agent","category":"Travel","url":null},{"id":4,"title":"The skinny 
confidential","pBody":"lifestyle 
blog","category":"Lifestyle","url":"https://www.theskinnyconfidential.com"},
{"id":5,"title":"three years of travel","pBody":"Three years of travel and 
adventure from around the 
world","category":"Video","url":"https://www.youtube.com/watch?
v=wJF5NXygL4k"}]}

any help as to what I'm doing incorrectly or tips on where i can find more information will be greatly appreciated.



via David Parsonson

No comments:

Post a Comment