[英]How can I send array from controller angularjs to codeigniter controller?
[英]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匹配的結果。 所以我不知道如何為此編寫查詢。
$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']此值到節點。
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" }
<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.