无法从http.get调用中获取数据库中的数据

时间:2014-02-25 10:23:07

标签: javascript node.js angularjs mongodb express

嘿家伙我有问题,我不确定它有什么问题,我正在尝试从http数据库中获取数据并将其返回到前端。这是我到目前为止所拥有的

app.get('/contentHandler/post/frontPage', contentHandler.displayMainPage);

displayMainPage

 this.displayMainPage = function(req, res, next) {
        "use strict";
        posts.getPosts(10, function(err, result) { //the 10 is limit to 10 post
            "use strict";
            if(err) return next(err);
            res.send(200,result); // send it to front end 
        });
    }

getPosts

  this.getPosts = function(num, callback) {
        "use strict";
        posts.find().sort('date', -1).limit(num).toArray(function(err, items) {
            "use strict";

            if (err) return callback(err, null);

            console.log("Found " + items.length + " posts");

            callback(err, items);
        });


    }

前端(角度控制器)

function IndexCtrl($scope, $http) {
  $http.get('/contentHandler/post/frontPage').
      success(function(data) {
        alert(data); // alert nothing/blank 
      }).error(function(err) {

      });


}

2 个答案:

答案 0 :(得分:0)

$ http * *之后删除逗号(,)。因为单个逗号有助于不允许进行下一个过程:p lol

尝试此代码而不是代码

function IndexCtrl($scope, $http) {
  $http.get('/contentHandler/post/frontPage').
      success(function(data) {
        alert(data); // alert nothing/blank 
      }).error(function(err) {

      });


}

答案 1 :(得分:0)

在$ http之后删除逗号(,)然后尝试你的代码。

相关问题