【问题标题】:Mongoose and angular filter results only with photo猫鼬和角度过滤器结果仅带有照片
【发布时间】:2016-04-21 11:05:18
【问题描述】:

我开发了一个过滤器复选框,将参数传递给“仅使用照片过滤结果”。

如果我把下面的代码直接放在服务器上可以工作,但是通过 angular 会出错。

还有其他方法可以做到这一点吗?

$scope.query = {
    picture: { $exists: true }
};

我的服务器路由器:

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

    console.log(req.query);

    User.find(req.query, function(err, users) {
        if (err) {
            res.send(err);
        }
        res.json(users);
    })
    .sort({ rating: -1 });
});

还有我的控制器复选框:

<md-checkbox class="md-primary" ng-model="query.picture" ng-change="loadusers()">filter results only with photo</md-checkbox>

我的函数检索服务器

$scope.loadusers = function(){

        $ionicLoading.show({
            template: 'Carregando...'
        });

        diaristajaAPI.all('users').getList($scope.query)

        .then(function(users){
            $ionicLoading.hide();
            $scope.listusers = users;
        })

        .catch(function(error) {
            $ionicLoading.hide();
            if (error.error) {
                // Popup error - invalid redirect_uri, pressed cancel button, etc.
                $mdToast.showSimple(error.error);
            } else if (error.data) {
                // HTTP response error from server
                $mdToast.showSimple(error.data.message, error.status);
            } else {
                $mdToast.showSimple(error);
            }
        });

    };

【问题讨论】:

  • 我们需要显示您如何从 Angular 与服务器通信的代码
  • 好的,走了!我的功能

标签: angularjs node.js express mongoose


【解决方案1】:

为复选框使用布尔值。

$scope.query = {picture: true};

然后在服务器上就很简单了

req.query.picture = {$exists: !!req.query.picture};

【讨论】:

  • 感谢您的快速回复!它部分工作,实现了你去的 o'que,现在只列出没有照片的用户。如果我把它们拿走!!它会给我一个角度错误。
  • 我需要复选框,在有照片的用户和所有照片和没有照片的用户之间进行切换。
  • 我试试这个 if(req.query.picture) req.query.picture = { $exists: true, $ne: [] };
猜你喜欢
  • 2017-07-18
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 2017-01-06
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多