将数据恢复到控制器的问题

时间:2014-05-04 07:22:27

标签: javascript node.js angularjs mongodb mongoose

我很难将数据库信息恢复到控制器中。

我的控制器使用

调用极客服务
$scope.dataValues = Geek.get();

然后该服务:

    get : function() {
        return $http.get('/api/geeks');
    },

然后我的路线确实。

app.get('/api/geeks', function(req, res) {

    // use mongoose to get all nerds in the database
    Route.find({},function(err,docs) {
        res.json(docs);
    });

});

我很困惑,当呼叫回到控制器时,那里什么也没有,页面看起来像。 http://prntscr.com/3g0whh

如果我在路由中放置了console.info(docs),则打印出来。

但是它似乎没有回到控制器,这使得dataValues = {}

[ { _id: '53639917f4ae962320d1711a',
    MailItem: 
     { objectId: 'ObjectRef',
       volume: '100',
       weight: '5',
       priority: 'InternationalAir',
       route: '',
       dateEntered: 'Thursday',
       timeEntered: '20/05/1415:35',
       price: '7.50',
       dimension: { height: '4', width: '4', length: '4' } } } ]

Geek.html

<ul>
    <li ng-repeat="mailItem in dataValues">
        {{mailItem.price}}
    </li>
</ul>

{{dataValues}}

1 个答案:

答案 0 :(得分:2)

$http.get返回一个承诺,因此您需要使用promise api。

试试这个;

Geek.get().then(function(response){
   $scope.dataValues = response.data;
});
相关问题