0

I am creating an AngularJS client and connecting it to a REST API I have running on NodeJS. I do GET requests to get the data by going to the URL and then using the stock symbol i.e. localhost:8080/stocks/aapl for Apple's share data. In AngularJS I am using the $http service and then doing data-binding like this:

var restURL = "http://localhost:8080/stocks/" + searchSymbol;

$http({
    method: 'GET',
    url: restURL
}).then(function successCallback(response) {
    //Variables that are going to be used
    var data, symbol, companyName;
    //Parsing the data
    data = angular.fromJson(response);
    //Putting the data into the variables
    symbol = data.symbol;
    companyName = data.name;
    //Putting data from variables into Angular code
    $scope.companyName = companyName;
    $scope.symbol = symbol;
}, function errorCallback(response) {
    var message = "Error getting response from server";
    $scope.errorMessage = message;
});

and this is what the view component looks like:

<h1>Search Stocks</h1>
<p>Search for stocks using the text box below</p>

<p><input type="text" ng-model="searchSymbol" placeholder="Search by Symbol"></p>
<p><button ng-click="search()">Search</button></p>

<p> {{ symbol }}</p>
<p>{{ companyName }}</p>

<p>{{ errorMessage }}</p>

When I click the button, the GET request is reflected on the NodeJS server as it shows the company data in the terminal view however I can't seem to get it parsed in Angular.

4
  • Console out the value of data after this line: data = angular.fromJson(response);. What is the value of the object? Commented Dec 14, 2015 at 14:37
  • Debug on first line when you get back response and check what it is. Btw, you can just write $http.get(restUrl).then(.... Commented Dec 14, 2015 at 14:41
  • @SeanPerkins This is the result I get {data: {symbol: "AAPL", name: "Apple Inc.", price: 110.949997}, status: 200, headers: function, config: Object, statusText: "OK"} Commented Dec 14, 2015 at 14:41
  • First glance looks like some issue with your searchSymbol ng-model. Not able to convert the data its getting from the node controller properly. Try out a JS Fiddle and post that would be easier for ever to help you. With Angular controller and all.. Commented Dec 14, 2015 at 14:42

1 Answer 1

2

Since your response is an object which has a property data in it which contains symbol, your symbol assignment symbol = data.symbol; needs to change to symbol = data.data.symbol; And likewise for companyName (use data.data.name).

See if that works.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.