【问题标题】:How to do 'show all posts except mine' in MEAN stack?如何在 MEAN 堆栈中“显示除我之外的所有帖子”?
【发布时间】: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


【解决方案1】:

collectedThoughtsIndexuserThoughtsIndex 路由从 url 获取 userId 参数,但 ma​​in 路由不使用此类参数。

您的 api 调用获取您从 stateProvider 生成的查询对象并调用服务器。但是,由于您在 ma​​in 路由中没有要生成的查询参数,因此您必须在没有任何查询参数的情况下进行 api 调用。

你必须以这种方式更新后端-

  • 当没有传递查询参数时
  • 如果用户登录
    • 返回除他之外的所有想法
  • 其他
    • 返回所有想法

我希望这对你有意义。 :)

【讨论】:

  • 我真的是 Angular 的新手,所以我无法自己弄清楚。但我不知道 userId 参数是从 url 中获取的,所以我最终用 '/users/:userId/others' url 创建了 'othersThoughts' 状态。我仍然想使用'/' url,但我不知道该怎么做。但是你的回答对我帮助很大。谢谢!
  • 很高兴能为您提供帮助,如果它解决了您的问题,您可以接受我的回答 :)
猜你喜欢
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 2015-03-17
  • 2019-08-19
相关资源
最近更新 更多