utkarsh tyagi
utkarsh tyagi

Reputation: 655

making a call to node.js server

I am learning MEAN stack, so in this case, i am making a call to node.js server

Server.js

var express = require('express');
var app = express();

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

app.get('/', function(req, res){
    res.send("Received the request");
});

app.use(express.static(__dirname + '/'));
app.listen(process.env.PORT || 5000);

Hosted the server on : https://todoserver.herokuapp.com/

So when i am making a call from angular js.

Angular js service:

TodoService.$inject = ['$http','ApiBasePath'];
function TodoService($http,ApiBasePath){
    var service = this;
    service.getItems = function(){
        var response = $http({
          method: "GET",
          url: (ApiBasePath + "/"),
        }); 
        console.log(response);
    };

    service.viewItems = function(){
        return items;
    };
};

ApiBasePath = https://todoserver.herokuapp.com

Hosted on: https://utkarsh17ife.github.io/todo/

I am receiving the response from server like this:

d
$$state:Object
error: function(a)
success:function(a)
__proto__:Object

Which should be like this:

config:Object
data:"Received the request"
headers:function(d)
status:200
statusText:"OK"

Which is present i go in d>$$state>Value. I think because of some reason i am not getting the response on correct level. Please tell me what i should do to get the correct reponse object.

Thanks in advance

Upvotes: 0

Views: 41

Answers (1)

rckrd
rckrd

Reputation: 3344

The $http service returns a promise, you can find the docs here: https://docs.angularjs.org/api/ng/service/$http. You are logging the promise.

You need to chain a success callback in order to log the actual response, as described here: https://docs.angularjs.org/api/ng/service/$http#general-usage

 // Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    console.log(response);
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

Upvotes: 1

Related Questions