2014-02-25 72 views
1

嘿傢伙我有問題,我不知道它有什麼問題,我試圖從數據庫獲取數據在http調用並將其返回到前端。這是我迄今爲止無法從http.get調用數據庫獲取數據

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); 
     }); 


    } 

前端(角JS控制器)

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

     }); 


} 

回答

0

刪除逗號(,)的$ HTTP * 後*。因爲一個逗號有助於不允許下一道工序:對笑

試試這個代碼,而不是你的代碼的

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

     }); 


} 
+0

即使我刪除逗號我仍然得到相同的結果,這是什麼也沒有 –

0

刪除逗號(,)之後$ HTTP,然後嘗試你的代碼。

+0

即使我刪除逗號我仍然得到相同的結果,這是什麼 –