res.json(item)以[object]响应?
我有一个平均堆栈,其中我使用ng repeat循环数据库中的项,{{items.value}}填充一些div上的文本。每个div都有res.json(item)以[object]响应?,json,node.js,angularjs,express,mongoose,Json,Node.js,Angularjs,Express,Mongoose,我有一个平均堆栈,其中我使用ng repeat循环数据库中的项,{{items.value}}填充一些div上的文本。每个div都有ng click=“getItem(item.\u id)”来触发角度页面重定向到 // dynamic pages for each ITEM, on ng-click // from $routeParams.itemID in Ctrl .when('/:itemID', { templateUrl: 'views/item.html', co
ng click=“getItem(item.\u id)”
来触发角度页面重定向到
// dynamic pages for each ITEM, on ng-click
// from $routeParams.itemID in Ctrl
.when('/:itemID', {
templateUrl: 'views/item.html',
controller: 'ItemElementsController'
})
创建、全部检索和删除都非常有效。。不过,retrieving One是用[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);
}
}
});
$routeParams.itemID目前没有itemID参数。当您的路由为“/:itemId”时,它将填充数据,但现在您位于“/list”或类似的位置(我看不到您所有的路由)
谢谢你更好的练习。。我仍然以未定义的形式获取数据,即使url读取。。。有什么建议吗?您能在$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中的项目数据?
.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);
};