【问题标题】:Issue getting the data back to the controller将数据返回到控制器的问题
【发布时间】:2014-06-20 15:31:23
【问题描述】:

我无法将数据库信息返回到控制器中。

我的控制器调用极客服务,用

$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' } } } ]

极客.html

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

{{dataValues}}

【问题讨论】:

    标签: javascript node.js angularjs mongodb mongoose


    【解决方案1】:

    $http.get 返回一个promise,所以你需要使用promise api。

    试试这个;

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

    【讨论】:

    • 嘿,这很有道理。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2015-11-15
    • 2017-12-23
    • 2019-11-21
    相关资源
    最近更新 更多