【问题标题】:res.json(item) responds with [object Object]?res.json(item) 以 [object Object] 响应?
【发布时间】:2014-05-20 11:18:43
【问题描述】:

我有一个 MEAN 堆栈,我在其中使用 ng-repeat 循环遍历数据库中的项目,并且 {{ items.value }} 在某些 div 上填充文本。每个 div 都有 ng-click="getItem(item._id)" 来触发一个角度页面重定向到

// dynamic pages for each ITEM, on ng-click
// from $routeParams.itemID in Ctrl
.when('/:itemID', {
    templateUrl: 'views/item.html',
    controller: 'ItemElementsController'
}) 

创建、检索全部和删除效果很好。但检索一个是用 [object Object] 响应的,所以 localhost:8080/undefined 和角度绑定 {{ items.value }} 没有填充

app.get('/api/items/:item_id', function(req, res) {

    // use mongoose to get the one emotion from the database
    Item.findById({
        _id : req.params.item_id
    },

    function(err, item) {

        // if there is an error retrieving, send the error. nothing after res.send(err) will execute
        if (err) {
          res.json({ error: err }); 
        } else {
            res.json(item); // return the item in JSON format
        }
    });
});

在 ItemElementsController 中:

angular.module('ItemElementsCtrl', [])

// inject the Item service.factory into our controller
.controller('ItemElementsController', function($scope, $routeParams, $location, $http, ItemElements, isEmptyObjectFilter) {

        // GET by ID ==================================================================
        // get an Item after clicking it
        $scope.getItem = function(id) {
                ItemElements.getOne(id)
                        // if successful getByID, call our function to get the Item data
                        .success(function(data) {
                                // assign our Item
                                $scope.item = data;
                                // for use with a parameter in appRoutes.js using itemID as the variable
                                $scope.itemID = $routeParams.itemID;
                                // redirect
                                $location.path('/' + $routeParams.itemID);
                        })
                        .error(function(data) {
                                console.log('Error: ' + data);
                        });
        };
});

还有 ItemElementsService:

angular.module('ItemElementsService', [])

    // super simple service
    // each function returns a promise object 
    .factory('ItemElements', function($http) {
        return {
            getOne : function(id) {
                return $http.get('/api/items/' + id);
            }
        }
    });

【问题讨论】:

    标签: json node.js angularjs express mongoose


    【解决方案1】:

    $routeParams.itemID 目前没有 itemID 参数。当您的路线为 '/:itemId' 但现在您位于 '/list' 或类似的位置时(我没有看到您的所有路线),它将填充数据。

    .controller('ItemElementsController', function($scope, $routeParams, $location, $http, ItemElements, isEmptyObjectFilter) {
        if ($routeParams.itemID !== undefinned){
                 ItemElements.getOne($routeParams.itemID)
                        // if successful getByID, call our function to get the Item data
                        .success(function(data) {
                                // assign our Item
                                $scope.item = data;
                                // for use with a parameter in appRoutes.js using itemID as the variable
                                $scope.itemID = $routeParams.itemID;
                        })
                        .error(function(data) {
                                console.log('Error: ' + data);
                        });
        }
        // GET by ID ==================================================================
        // get an Item after clicking it
        $scope.getItem = function(id) {
                $location.path('/' + id);
        };
    

    【讨论】:

    • 感谢您提供更好的做法。我仍然将数据返回为未定义,即使网址显示为localhost:1122/undefined ...有什么建议吗?
    • 能否在 $scope.getItem = function(id) {console.log(id); 之后添加控制台日志...} 并显示结果。看来您的 id 参数有问题。
    • 它现在正在记录 id!谢谢你.. 和 localhost:1122/5343a882f35199e102000001,但是我们重定向到的 item.html 文件中的角度绑定 {{ item.value }} 没有填充,并且节点的 console.log(emotion) 仍然未定义跨度>
    • 没关系,因为你使用了$location.path(...),这个函数会刷新你当前的$scope。
    • 我们如何获得应该在 $scope.emotion = data 中的项目数据?
    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 2018-03-19
    • 2020-11-17
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 2015-04-15
    • 2018-04-03
    相关资源
    最近更新 更多