【发布时间】:2016-04-03 03:04:08
【问题描述】:
我基于this tutorial 创建了我的 MEAN 堆栈应用程序。 这是我在 /client/app/thoughtsIndex/ 中的thoughtsIndex.js。您可以将“想法”视为帖子。
angular.module('meanGoroApp')
.config(function ($stateProvider) {
$stateProvider
.state('main', {
url: '/',
templateUrl: 'app/thoughtsIndex/thoughtsIndex.html',
controller: 'ThoughtsIndexCtrl',
resolve: {
query: function($stateParams) {
return {
// I need help here!!
}
}
}
})
.state('collectedThoughtsIndex', {
url: '/users/:userId/collected',
templateUrl: 'app/thoughtsIndex/thoughtsIndex.html',
controller: 'ThoughtsIndexCtrl',
resolve: {
query: function($stateParams) {
return {
collectedBy: $stateParams.userId
}
}
}
})
.state('userThoughtsIndex', {
url: '/users/:userId',
templateUrl: 'app/thoughtsIndex/thoughtsIndex.html',
controller: 'ThoughtsIndexCtrl',
resolve: {
query: function($stateParams) {
return { user: $stateParams.userId };
}
}
});
});
“collectedThoughtsIndex”状态和“userThoughtsIndex”状态工作正常。
问题是,当状态为“主要”时,我想显示数据库中的所有想法,除了当前用户的想法。如果没有当前用户,我想在 db 中显示所有想法。
I asked to the tutorial author,他说试试 {user: {$ne: $stateParams.userId}}。但这给了我这个错误。
Unhandled rejection CastError: Cast to ObjectId failed for value "[object Object]" at path "user"
at MongooseError.CastError (/Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/error/cast.js:19:11)
at ObjectId.cast (/Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/schema/objectid.js:147:13)
at ObjectId.castForQuery (/Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/schema/objectid.js:187:15)
at cast (/Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/cast.js:141:34)
at Query.cast (/Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/query.js:2570:10)
at Query.find (/Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/query.js:1087:10)
at /Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/query.js:2164:21
at Query.exec (/Volumes/Garage/GORO/mean_goro/node_modules/mongoose/lib/query.js:2157:10)
From previous event:
at index (/Volumes/Garage/GORO/mean_goro/server/api/thought/thought.controller.js:78:55)
at Layer.handle [as handle_request] (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/layer.js:95:5)
at next (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/layer.js:95:5)
at /Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:330:12)
at next (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:176:3)
at router (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:46:12)
at Layer.handle [as handle_request] (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:312:13)
at /Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:330:12)
at next (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:271:10)
at Layer.handle [as handle_request] (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/layer.js:91:12)
at trim_prefix (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:312:13)
at /Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:330:12)
at next (/Volumes/Garage/GORO/mean_goro/node_modules/express/lib/router/index.js:271:10)
到目前为止,我尝试过
{user: {$ne: $stateParams.userId}}
{user: {$not: $stateParams.userId}}
{$ne: {user: $stateParams.userId}}
{$not: {user: $stateParams.userId}}
但这些都不起作用。 请帮帮我..我很绝望:-(
++ 这是我的“thoughtsIndex.controller.js”文件。
'use strict';
angular.module('meanGoroApp')
.controller('ThoughtsIndexCtrl', function ($scope, $http, Auth, $location, query, socket) {
$scope.busy = true;
$scope.noMoreData = false;
$http.get('/api/thoughts', {params: {query: query}}).success(function(thoughts) {
$scope.thoughts = thoughts;
socket.syncUpdates('thought', thoughts);
if($scope.thoughts.length < 20) {
$scope.noMoreData = true;
}
$scope.busy = false;
});
$scope.nextPage = function() {
if($scope.busy) { return; }
$scope.busy = true;
var lastId = $scope.thoughts[$scope.thoughts.length-1]._id;
var pageQuery = _.merge(query, {_id: {$lt: lastId}});
$http.get('/api/thoughts', {params: {query: pageQuery}}).success(function(thoughts) {
$scope.thoughts = $scope.thoughts.concat(thoughts);
$scope.busy = false;
if(thoughts.length === 0) {
$scope.noMoreData = true;
}
});
}
$scope.isOwner = function(obj) {
return Auth.isLoggedIn() && obj && obj.user && obj.user._id === Auth.getCurrentUser()._id;
}
$scope.isCollected = function(obj) {
return Auth.isLoggedIn() && obj && obj.collectedBy && obj.collectedBy.indexOf(Auth.getCurrentUser()._id) !== -1;
}
$scope.isLoggedIn = function() {
if(!Auth.isLoggedIn()) {
$location.path('/login');
$location.replace();
return;
}
}
$scope.submit = function() {
$http.post('/api/thoughts', $scope.thought).success(function() {
$location.path('/users/' + Auth.getCurrentUser()._id);
$scope.clearInput();
});
}
$scope.clearInput = function() {
$scope.thought.main_sentence = null;
$scope.thought.sub_sentence = null;
}
});
【问题讨论】:
-
请发布您的 ThoughtsIndexController 文件
-
@atefth 如您所述,我发布了控制器文件。我希望你能再次检查。
标签: angularjs mongodb angular-ui-router mean-stack