【问题标题】:Filtering MongoDB data on the front end在前端过滤 MongoDB 数据
【发布时间】:2017-09-07 22:37:48
【问题描述】:

我对 MongoDb 有一些小问题。我有 2 个收藏:拍卖和用户。在“用户”中,我显然存储了用户(邮件、姓名……等)。在拍卖中,我存储了产品(创建日期、创建者、关注者、邀请、图像)。现在,我想在每个用户的仪表板中显示他关注的产品(当他点击按钮关注时,他的名字存储在“FollowedBy”中)。如果我必须在后端过滤,一切都很清楚,相反,我想从数据库中加载所有数据,然后在前端使用 angularJs 对其进行过滤。

使用 createdBy 这样的简单字段很容易:如果我有一个名为“Alex”的用户(例如),我只能在 createdBy 与 Alex(或其他所有用户)匹配时显示数据。但同样的事情不适用于像“FollowedBy”这样的数组字段。如果在产品“表”中创建者:“Mark”,FollowedBy:“Alex”和“Jack”,我可以轻松过滤所有产品以仅显示 Mark 的产品,但我不明白如何仅显示产品Alex,因为这个字段是一个数组。

这是我的后端路线。

           app.get('/api/dashboard', function (req, res) {

    AllAuctions
        .find({}, function (err, auctions) {
            if (err)
                res.send(err);
            console.log(auctions);
            res.send(auctions);

        });
});

我用 $http 和 angularJs 加载它:

         $http.get('/api/dashboard').then(function (AllAuctions) {
           $scope.allauctions = AllAuctions.data;

       });

如果我必须过滤“createdBy”字段,我可以这样做:

           <div class="panel panel-default" >
    <div class="panel-heading">My Auctions</div>
    <div class="panel-body">
        <ul class="list-group" ng-repeat="allauction in allauctions">
            <li class="list-group-item" ng-show="allauction.createdBy=='Alex'">
                    <img ng-src="{{allauction.imgUrl}}">
                    <div class="title"> {{allauction.title}} </div>

</li>
</ul>
</div>
</div>

但我不明白如何过滤这样的字段:

                 FollowedBy: [
                      Shelly.name,
                      Dario.name
                 ]

仅显示 Dario 之后的产品。

【问题讨论】:

  • 发布你的代码到现在你做了什么

标签: angularjs node.js mongodb mongoose


【解决方案1】:

如果你想在 html 中做,你可以做类似的事情

ng-show="allauction.FollowedBy.indexOf('Alex')>=0"

但是,当您拥有多个过滤器时,它很快就会变得硬核。您应该在 javascript 中定义一个过滤函数,该函数将计算一个过滤后的数组,您可以在该数组上执行简单的 ng-repeat。或者,您也可以在您的 allauction 对象中添加一个标志,并基于此标志执行 ng-if/ng-show。例如:

function updateFilteredData()
{
    $scope.filteredData = [];
    for(var i = 0; i< $scope.allauction; i++)
    {
         var auction = $scope.allauction[i];
         if(auction.createdBy !== currentUser.name) //I assume currentUser is json object for your Alex user
             continue;
         if($scope.followByFilter && auction.FollowedBy.indexOf($scope.followByFilter) < 0)
              continue; //I assume followByFilter is a string containing the name of a follower you are trying to filter

         $scope.filteredData.push(auction);
    }

}

您需要在 $http get 中调用该函数一次,并且每次过滤条件更改(例如:followByFilter 名称更改...)。 Ofcorse 你应该 ng-repeat 对 filtersData 数组而不是 allauction。

【讨论】:

  • 好吧,我明白了,我不一定要用 html 来做。但是我尝试构建该功能,但没有成功。你能给我举个例子吗?在我的 $http get 中,我将数据保存在 alluctions 中,你能给我举个例子来只加载 Alex 后面的产品吗?和其他问题:我必须为每个用户构建一个功能?
  • 为什么不将用户名作为输入,然后将其存储在 localstorage 或 $rootScope 上,然后像 ng-if="allaction.FollowedBy === user.name" 进行比较,其中 "user" 是 rootScope 对象
  • FollowedBy 是一个数组......并且在 html 中过滤非常慢。最好在js中过滤。
猜你喜欢
  • 2020-03-15
  • 1970-01-01
  • 2014-04-18
  • 2019-11-05
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 2019-01-14
  • 2021-07-26
相关资源
最近更新 更多