简体   繁体   中英

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. So i don't know how to write query for this.

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.

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. 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.

{ "_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

<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

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');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM