0

I am building an application using angularjs nodejs expressjs and mysql.

Right now i am facing a problem where i am getting empty response from Node while in the node console i saw it successfully retrieve the data from mysql.

angularjs

app.controller('MainCtrl', function($scope, $resource, Map) {
  $scope.fetchdata = {};
  var Fetchdata = $resource('http://localhost:8080/fetchnote');
  Fetchdata.query(function(results) {
      $scope.fetchdata = results;
  })
  console.log(JSON.stringify($scope.fetchdata));
})

nodejs

app.get('/fetchnote', function(req, res) {
    connection.query('SELECT * from note', function(err, rows, fields) {
        if (!err) {
            console.log(rows);
            res.json(rows);
        } else {
            console.log(err);
        }
    });
});

if i pass http://localhost:8080/fetchnote directly to the browser address bar, then it would show all records, same goes to the node console, saw all records, while not in angularjs. No error in javascript console as well, just empty records.

Anyone can give some idea?

4
  • are you facing any cross domain issue in network console? Commented Nov 18, 2016 at 9:07
  • nope, no error at all. Commented Nov 18, 2016 at 9:11
  • Why don't you do an http get request in angular to match the verb in node of using resource? Commented Nov 18, 2016 at 9:13
  • @ArrowHead would you please show me how? kinda new to this stuff Commented Nov 18, 2016 at 9:14

1 Answer 1

1
app.controller('MainCtrl', function($scope, $resource, Map) {
  $scope.fetchdata = {};
  var Fetchdata = $resource('http://localhost:8080/fetchnote');
  Fetchdata.query(function(results) {
      $scope.fetchdata = results;
      console.log(JSON.stringify($scope.fetchdata));
  })
})

In the browser you can press F12 and see the network tab what requests are made (filter for xhr). I prefer Chrome as that has a nice preview of json resonses.

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

4 Comments

can you confirm that the data is returned from the xhr while calling from angular? You can view the response at Network-> XHR Tab of chrome dev tools
@JosephGoh take a closer look at the position of console.log in your code and in this answer.
@PaulsonPeter okay, i see the diff now, but why can't i access it outside Fetchdata.query(... ? If i can't access it, this mean i can't do anything with the data which i want iterate it.
It is an ajax/ asynchronous request. The codes written below will be executed before getting the result of ajax call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.