1

I am building an app using Node JS and Angular JS. I got the data as response from the Back end. But failed to show the data in UI.

my code is like below,

var app = angular.module('app', [])

app.controller('appCtrl', ['$scope', '$http', 'storeData', function ($scope, $http, storeData) {

    $scope.searched = []
    $scope.locationSearch = function () {
        google.maps.event.addDomListener(window, 'load', function () {

            // get data from Google Api
            $scope.places = new google.maps.places.Autocomplete(document.getElementById('searchPlaces'));
            google.maps.event.addListener($scope.places, 'place_changed', function () {

                // set the corresponding field 
                $scope.place = $scope.places.getPlace();
                $scope.address = $scope.place.formatted_address;
                $scope.latitude = $scope.place.geometry.location.lat();
                $scope.longitude = $scope.place.geometry.location.lng();
                $scope.mesg = "Address: " + $scope.address + "\nLatitude: " + $scope.latitude + "\nLongitude: " + $scope.longitude;

                // call the service
                $scope.searched = storeData.putData({ "userId": '6235', "place": $scope.address, "latitude": $scope.latitude, "longitude": $scope.longitude })

            })
        })
    }
}])

app.service('storeData', ['$http', function ($http) {
    // this.searchResults = []
    this.putData = function (place) {

        // call the back end
        return new Promise(function (resolve, reject) {
            $http({
                method: 'POST',
                url: '/store',
                headers: 'Content-Type: application/json',
                data: place
            })
        }).then(function (success) {
            console.log(success.data)
            // this.searchResults = success.data
            return success.data
        }).catch(function (err) {
            console.log('unable to process request')
        })
    }
}])

I am using Google Location api to get a place search box. All the data is ok. They are sent to the back end correctly. And response is coming good.

I have used Chrome Developer tools to check the response. Response it Good. But I am unable to show it in the UI.

My HTML code is like below,

<table class="table" ng-controller="appCtrl">
                            <tr ng-repeat="result in searched">
                                <td>{{result.place}} </td>
                                <td>{{result.latitude}} </td>
                                <td>{{result.longitude}} </td>
                            </tr>
                        </table>

Somehow I have to manage the $scope.searched to show the data. Can you please tell what is wrong in the code ?? And how to correct it?? I want to use the Service to call the back end and send the output to the Controller.

5
  • 1
    storeData::putData return a promise Commented May 4, 2017 at 13:35
  • 2
    Possible duplicate of AngularJS factory http returns empty Commented May 4, 2017 at 13:39
  • I strongly recommend you to have a look at the following : stackoverflow.com/questions/14994391/… Commented May 4, 2017 at 13:42
  • 1
    You don't need to wrap $http.post into a promise. It's already a promise. Commented May 4, 2017 at 13:44
  • a controller is always run after the window has been loaded. You don't need to register such event. You're just leaving angular scope, and angular won't notice the controller updated some of the data that need to be displayed in the view Commented May 4, 2017 at 13:45

1 Answer 1

1

You need to resolve promise correctly and assign the resolved data to your $scope variable. Currently your $scope variable has promise and not the data. Try this:

 var searchedPromise = storeData.putData({ "userId": '6235', "place": $scope.address, "latitude": $scope.latitude, "longitude": $scope.longitude });
    searchedPromise.then(storeData.promiseResolvingFnInService);
    $scope.searchResults = storeData.searchResults;

and

app.service('storeData', ['$http', function ($http) {
    this.searchResults = [];
    this.promiseResolvingFnInService =  function(response){
      this.searchResults = response.data; //assuming array is inside data prop of API response.
   }
    this.putData = function (place) {

        // call the back end
        return 
            $http({
                method: 'POST',
                url: '/store',
                headers: 'Content-Type: application/json',
                data: place
            });
    }
}])

This should help.

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

3 Comments

thanks this works. Another thing, can I store it in a Array of the storeData service and access it in my HTML. If yes, can you give me the code ?
You can store anything in your service. But in your HTML you can use only the data/variable/objects which you have on your $scope
Updated my answer for your use. Now the promise resolution is done by service function and the scope array is assigned to service array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.