[英]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. 所以我不知道如何为此编写查询。
$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']此值到节点。
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 运营商使用$后的截图
<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.