0

I've got an app which makes calls to an api and shows data about that info: first, it takes a username from an input in the client and posts it to the server. In this step if I check if the data has been sent alright to the server everything seems fine.

AngularJs part:

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

app.controller("InfoController",['$scope','$log','$http','$filter',function($scope,$log,$http,$filter){ 

    $scope.info = {
        summonerName: '',
    };

    $scope.info.onClick = function(){
        $scope.info.summonerName = $scope.info.summonerNameBox;

        $http.post('/api/getSummonerName', $scope.info);

    $http.get('/api/summonerData')
        .success(function(data, status){
        $scope.info.display = data;
    }).error(function(data,status){
        alert("Error: " + status);  
    });
    };
}])

Nodejs:

var summonerName;
var summonerId = '';

app.post('/api/getSummonerName', function (req, res){
    summonerName = req.body.summonerName;
    console.log("Post getSummonerName " + summonerName);
});

app.get('/api/summonerData', function(req,res){
    LolApi.Summoner.getByName(summonerName, function(err, summoner) {
        if(!err) {
            res.json(summoner);
            summonerId = req.body.summoner;
            console.log(summonerId);
            if(err)
                res.send(err)
        }
    })
});

When I have this data in the server, I make a call to an api for getting this user's info, and everything goes fine as well.

The problem comes when I want to store an specific field from this json. When I want to store it in a variable, everything I get is "undefined" :/ (Once I achieved getting "[Object object]", but can't get it anymore...)

Any idea?

Thank you all.

1 Answers1

0

In my opinion, this isn't the way you should do it.

If you want to do a get on a summoner, you need to just use the get method. Especially if you're dealing with multiple users.

You might want to pass data in the get request like specified in this answer and ditch the post request: AngularJS passing data to $http.get request

That way, you're not dependent on the memory and you're able to modularize it :)

Community
  • 1
  • 1
CherryNerd
  • 1,258
  • 1
  • 16
  • 38