繁体   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

我正在从角度控制器向节点发送数组值,看起来如下['5732c95d599bfcc031013929','5732c8e6599bfcc031013925']使用此值我必须搜索与mongoDB中的两个id匹配的结果。 所以我不知道如何为此编写查询。

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

在上面的代码中,我发送的proID包含['5732c95d599bfcc031013929','5732c8e6599bfcc031013925']此值到节点。

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

在此节点中,值pId包含我从角度控制器发送的数组。 在查询中,它只获取数组的第一个值并抛出结果,但第二个不使用。

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" }

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

运营商使用$后的截图

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>

您需要在这样的find使用$ in运算符

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

评论后编辑

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

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