简体   繁体   English

我如何从角度控制器发送数组值到节点,并使用该数组值我必须从mongoDB搜索结果

[英]How do i send array values from angular controller to node and using that array values i have to search result from mongoDB

I am sending array values from angular controller to node, that is look like following [ '5732c95d599bfcc031013929', '5732c8e6599bfcc031013925' ] using this value i have to search result that matches both id in mongoDB. 我正在从角度控制器向节点发送数组值,看起来如下['5732c95d599bfcc031013929','5732c8e6599bfcc031013925']使用此值我必须搜索与mongoDB中的两个id匹配的结果。 So i don't know how to write query for this. 所以我不知道如何为此编写查询。

controller.js controller.js

$http({
            url: 'http://192.168.2.8:7200/api/manage-product',
            method: 'POST',
            data: {userId:vm.uid, code:vm.purchased, code1:vm.contributed}
        }).success(function(res) {
            //$scope.productlist = res;
            vm.result = res.result;
            console.log(vm.result);
            vm.count=vm.result.length;
            //console.log(vm.count + "New")

            vm.showPurchaserInfo = false;
            vm.showMessage = false;

            vm.info=[];
            vm.info[0]=vm.result[0]._id;
            vm.info[1]=vm.result[1]._id;

            if($scope.selectedCode == 2 || 3){
                if(vm.count >= 1 ){
                    $http({
                        url: 'http://192.168.2.8:7200/api/purchaserInfo',
                        method: 'GET',
                        params: {"proID" : vm.info}
                    }).success(function(res) {
                        vm.purchasedPeople = res.result;
                        console.log(vm.purchasedPeople);

                        if($scope.selectedCode == 2){
                            vm.showPurchaserInfo = true;
                        } else {
                            vm.showPurchaserInfo = false;
                        }
                    }, function(error) {
                        console.log(error);
                        alert('here');
                    });
                } else {
                    vm.result = res.result;
                    vm.count=vm.result.length;
                    console.log(vm.count);
                    if(vm.count == 0){
                        vm.showMessage = true;
                    } else {
                        vm.result=res.result;   
                        vm.showMessage = false; 
                    }
                }
            }
            //console.log(vm.result);
            //vm.docs=res.docs;
        }, function(error) {
            console.log(error);
            alert('here');
        });
    };

In the above code i am sending params of proID contains [ '5732c95d599bfcc031013929', '5732c8e6599bfcc031013925' ] this value to node. 在上面的代码中,我发送的proID包含['5732c95d599bfcc031013929','5732c8e6599bfcc031013925']此值到节点。

node.js 的node.js

router.get('/purchaserInfo',function(req,res){
    console.log(req.query.proID);
    var pId = req.query.proID;
    var findPurchaserInfo = function(db, callback) {
        var cursor =db.collection('purchased').find({purchasedItemID:pId}).toArray(function(err, docs){
            if(err){  
                callback(new Error("Some problem"));
            }else{
                callback(null,docs);
            } 
        });
    };

    MongoClient.connect(url, function(err, db) {
        assert.equal(null, err);
        findPurchaserInfo(db, function(err,docs) {
            db.close();
            if(err) return res.json({result:null})
            else
            return res.json({result: docs});
        });
    });     
});

In this node the value pId contains that array i had sent from angular controller. 在此节点中,值pId包含我从角度控制器发送的数组。 In the query it takes only first value of array and throw the result of that, but second one is not using. 在查询中,它只获取数组的第一个值并抛出结果,但第二个不使用。

mongoDb collection is look like following. mongoDb集合如下所示。

{ "_id": ObjectId("5733397f3e102e40365bb5de"), "quantity": "1", "store": "amazon", "purchasedPerson": "Admin", "email": "admin@gmail.com", "message": "I purchased this item for you", "terms": "yes", "purchasedItemID": "5732c95d599bfcc031013929", "purchasedDate": "2016-05-11T13:53:45.606Z", "ownerId": "56fe44836ce2226431f5388f" }

and

{ "_id": ObjectId("5733006ace25b9682abb7c89"), "quantity": "1", "store": "amazon", "purchasedPerson": "vinay", "email": "vinay@gmail.com", "message": "I purchased this item for you", "terms": "yes", "purchasedItemID": "5732c8e6599bfcc031013925", "purchasedDate": "2016-05-11T09:50:17.718Z", "ownerId": "56fe44836ce2226431f5388f" }

screenshot after $in operator use 运营商使用$后的截图

from.html from.html

<md-content  >  
<div class="md-padding"  layout="row"  layout-wrap >
    <md-card  ng-repeat="product in vm.result | filter:searchText" flex="40">
        <md-card-header>
            <md-card-header-text>
                <span class="md-title">{{ product.Product_Name }}</span>
            </md-card-header-text>
        </md-card-header>
        <img ng-src="{{ product.Image }}" class="md-card-image" alt="Image here">
        <md-card-title>
            <md-card-title-text>
                <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Brand:</span>  {{ product.Brand }}</span>
                <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Price: </span>  {{ product.Price }}</span>
                <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Capasity/Color:</span>  {{ product.Color }}</span>
                <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Url: </span>  <a href="{{ product.Url }}" target="_blank" >{{ product.Url | characters:100}}</a></span>
                <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Category: </span>  {{ product.Category }}</span>
                <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Description: </span>  {{ product.Description | characters:100}}</span>
            </md-card-title-text>
        </md-card-title>

        <div class="" ng-repeat="info in vm.purchasedPeople" ng-show="vm.showPurchaserInfo">
            <span layout="row"><hr flex/></span>
            <md-card-title >
                <md-card-title-text >
                    <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Purchased By:</span> {{info.purchasedPerson}} </span>
                    <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Store:</span> {{info.store}} </span>
                    <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Purchased Date:</span> {{info.purchasedDate | date:"MM/dd/yyyy 'at' h:mma"}} </span>
                    <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Guest's Note:</span> {{info.message}} </span>
                    <span><span class="md-subhead" style="font-weight: bold; padding:2px;">Purchase Quantity:</span> {{info.quantity}} </span>
                </md-card-title-text>
            </md-card-title>
        </div>

        <md-card-actions layout="row" layout-align="end center">
            <md-button class="send-button md-accent md-raised" ng-click="vm.editDialog($index,product._id)">Edit</md-button>
            <md-button name="ProductId" class="send-button md-accent md-raised" ng-click="remove(product._id,'{{$index}}')">Remove</md-button>
        </md-card-actions>
      </md-card>

You need to use $in operator in find like this 您需要在这样的find使用$ in运算符

db.collection('purchased').find({purchasedItemId:{$in: pId}}).toArray(...);

Edit after comments 评论后编辑

$http({
    url: 'http://192.168.2.8:7200/api/purchaserInfo',
    method: 'GET',
    params: { "proID": vm.info }
}).success(function(res) {
    var purchasedPeople = res.result;

    // iterate over people who purchased from currently logged in user (owner)
    for (var i = 0; i < purchasedPeople.length; i++) { 

        // iterate over owner's products       
        for (var j = 0; j < vm.result.length; j++) {

            // if vm.result[j].purchasedPeople doesn't exist create it otherwise use previously created      
            vm.result[j].purchasedPeople = vm.result[j].purchasedPeople || [];

            // if purchasedItemID is same as product id then add this person to this product
            if(purchasedPeople[i].purchasedItemID === vm.result[j]._id) vm.result[j].purchasedPeople.push(purchasedPeople[i]);

        }

    }

    if ($scope.selectedCode == 2) {
        vm.showPurchaserInfo = true;
    } else {
        vm.showPurchaserInfo = false;
    }
}, function(error) {
    console.log(error);
    alert('here');
});

暂无
暂无

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

相关问题 如何将数组从控制器angularjs发送到codeigniter控制器? - How can I send array from controller angularjs to codeigniter controller? 角度-如何查看过滤器结果数组中来自控制器的更改 - Angular - how can I watch a filter result array for changes from the controller 如何在angular.js中访问嵌套数组值? - How do i access nested array values in angular.js? 我如何从数组中获取两个值并将它们包含在angularjs控制器中的新函数中 - How can I get two values from an array and include them in a new function all in an angularjs controller 如何使用Node / Express和Angular从服务器向视图发送JSON对象 - How do I send a JSON object from server to my view using Node/Express and Angular 如何使用Angular JS以随机方式从数组中删除值? - How to remove values from array in random fashion using Angular JS? 从控制器填写值时,角度选项数组变得不确定 - Angular option array getting undefined when filling in values from controller 如何将带有数组的表单值传递给角度控制器 - How to pass the form values with array to angular controller 使用angular js仅从数组数组中获取值 - Get only values from the array of array using angular js 使用节点/角度,如何调用api(使用节点模块中的方法)并将数据发送到示波器? - Using Node/Angular, how do I make a call to an api (using methods from a node module) and have the data sent to the scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM