如何在 MEAN 堆栈中执行 'show all posts except mine'?
How to do 'show all posts except mine' in MEAN stack?
我基于 this tutorial 创建了我的 MEAN 堆栈应用程序。
这是我在 /client/app/thoughtsIndex/ 中的 thoughtsIndex.js。您可以将 'thought' 视为 post.
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' 状态工作正常。
问题是,当状态为 'main' 时,我想在数据库中显示所有想法,除了当前用户的想法。如果没有当前用户,我想在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}}
但其中 none 有效。
请帮助我..我很绝望:-(
++
这是我的 '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;
}
});
collectedThoughtsIndex 和 userThoughtsIndex 路由从 url 获取 userId 参数,但 main 路由没有这样的参数。
Your api call takes the query object that you generate from the stateProvider and makes a call to the server. However, since you have no query params to generate in the main route, you have to make the api call without any query params.
你必须以这种方式更新后端 -
- 当没有传递查询参数时
- 如果用户已登录
- return 除了他的所有想法
- 其他
- return所有想法
我希望这对你有意义。 :)
我基于 this tutorial 创建了我的 MEAN 堆栈应用程序。 这是我在 /client/app/thoughtsIndex/ 中的 thoughtsIndex.js。您可以将 'thought' 视为 post.
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' 状态工作正常。
问题是,当状态为 'main' 时,我想在数据库中显示所有想法,除了当前用户的想法。如果没有当前用户,我想在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}}
但其中 none 有效。 请帮助我..我很绝望:-(
++ 这是我的 '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;
}
});
collectedThoughtsIndex 和 userThoughtsIndex 路由从 url 获取 userId 参数,但 main 路由没有这样的参数。
Your api call takes the query object that you generate from the stateProvider and makes a call to the server. However, since you have no query params to generate in the main route, you have to make the api call without any query params.
你必须以这种方式更新后端 -
- 当没有传递查询参数时
- 如果用户已登录
- return 除了他的所有想法
- 其他
- return所有想法
我希望这对你有意义。 :)