简体   繁体   English

使用客户端发送的变量搜索MongoDB

[英]Search MongoDB using a variable sent from client

I'm using MongoJS in a Node project with Angular and trying to find documents based on a variable. 我在带有Angular的Node项目中使用MongoJS,并尝试基于变量查找文档。

server.js server.js

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

    db.Fruits.find({code:'Apple'}).forEach(function(err, docs){
        res.json(docs);
    });
});

Route 路线

.when('/find/:fruit', {
            templateUrl: 'views/find.html',      
            controller: 'findCtrl',
            resolve: {
                result: function(searchService) {
                    return searchService.getResult();
                }
            }
        })

service.js (client) service.js (客户端)

.factory('searchService', function($q, $http) {
        return {
            getResult: function() {
                return $http.get('/api/find')
                    .then(function(response) {
                        if (typeof response.data === 'object') {
                            return response.data;
                        } else {
                            return $q.reject(response.data);
                        }
                    })
            }     

        }         
    })

Controller.js Controller.js

.controller('findCtrl', function($scope, result) {
    $scope.result = result;
})

As you can see in server.js, I'm passing a static string 'Apple'. 如您在server.js中看到的,我正在传递静态字符串'Apple'。 I wanna replace that with the $routeparams ':fruit' which I've listed in Route. 我想用在Route中列出的$ routeparams':fruit'代替它。

Please help. 请帮忙。

You need to pass the url param :fruit to your searchService: 您需要将url param:fruit传递给searchService:

.when('/find/:fruit', {
    templateUrl: 'views/find.html',      
    controller: 'findCtrl',
    resolve: {
        result: function(searchService) {
            return searchService.getResult($route.current.params.fruit);
        }
    }
})

Search Service: 搜索服务:

.factory('searchService', function($q, $http) {
    return {
        getResult: function(fruit) {
            return $http.get('/api/find?fruit=' + fruit)
                .then(function(response) {
                    if (typeof response.data === 'object') {
                        return response.data;
                    } else {
                        return $q.reject(response.data);
                    }
                })
        }     

    }         
})

Server: I normally use mongoose but you could probably use .toArray as using forEach was iterating over the collection and attempting to end the response multiple times. 服务器:我通常使用猫鼬,但您可能可以使用.toArray,因为使用forEach遍历集合并尝试多次终止响应。

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

    db.Fruits.find({code:req.query.fruit}).toArray(function (err, result) {
        return res.json(result);
    });
});

EDIT: Just read that you're using mongojs You should be able to do: 编辑:只需阅读您正在使用mongojs,您应该能够:

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

    db.Fruits.find({code:req.query.fruit}, function (err, docs) {
        return res.json(docs);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongodb中客户端和服务器之间的搜索API - Search API between client and server in mongodb 使用mongodb从对象数组中的搜索中查找完全匹配 - Find an exact match from search in array of objects using mongodb 使用nodejs在整个集合中搜索(mongodb) - Search in whole collection(mongodb) using nodejs 保存后客户端未收到控制器发送的新消息 - Client not receiving new message sent from Controller after save 我如何从角度控制器发送数组值到节点,并使用该数组值我必须从mongoDB搜索结果 - How do i send array values from angular controller to node and using that array values i have to search result from mongoDB 如何将保存在MongoDB中的客户端的日期保留为日期? - How to keep date from client saved in MongoDB as date? 从客户端发送数据后如何从数据库取回数据? - How to get back data from DB after sent it from client side? 如何使用nodejs在mongodb中的两个不同集合中搜索值 - how to search for values in two different collections in mongodb using nodejs 从角客户端回送MongoDB GeoIp'near'查询 - Loopback MongoDB GeoIp 'near' query from angular client 客户端发送的请求在语法上不正确,而使用AngularJS通过其余部分调用WebService时 - The request sent by the client was syntactically incorrect while calling a WebService through rest using AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM