【问题标题】:Nodejs + Expressjs + Angularjs Empty Mysql Select ResultNodejs + Expressjs + Angularjs Empty Mysql 选择结果
【发布时间】:2017-04-02 01:32:59
【问题描述】:

我正在使用 angularjs nodejs expressjs 和 mysql 构建一个应用程序。

现在我面临一个问题,我从 Node 得到空响应,而在节点控制台中我看到它成功地从 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);
        }
    });
});

如果我将http://localhost:8080/fetchnote 直接传递到浏览器地址栏,那么它将显示所有记录,同样进入节点控制台,看到所有记录,而不是在 angularjs 中。 javascript控制台也没有错误,只是空记录。

谁能给点意见?

【问题讨论】:

  • 您是否在网络控制台中遇到任何跨域问题?
  • 不,完全没有错误。
  • 你为什么不做一个角度的http get请求来匹配使用资源节点中的动词?
  • @ArrowHead 你能告诉我怎么做吗?对这些东西有点陌生

标签: javascript mysql angularjs node.js


【解决方案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));
  })
})

在浏览器中,您可以按 F12 并查看网络选项卡发出的请求(过滤 xhr)。我更喜欢 Chrome,因为它可以很好地预览 json resonses。

【讨论】:

  • 你能确认从角度调用时数据是从xhr返回的吗?您可以在 Network-> XHR Tab of chrome dev tools 查看响应
  • @JosephGoh 仔细查看 console.log 在您的代码和此答案中的位置。
  • @PaulsonPeter 好吧,我现在看到了差异,但是为什么我不能在 Fetchdata.query(... 之外访问它?如果我无法访问它,这意味着我无法对我想要迭代它的数据做任何事情。
  • 这是一个ajax/异步请求。下面写的代码会在得到ajax调用结果之前执行。
猜你喜欢
  • 2015-07-17
  • 1970-01-01
  • 2011-02-27
  • 2012-12-22
  • 2016-03-12
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多