2016-09-21 60 views
0

我試圖保持我的表數據與數據庫數據同步。MEAN App - 數據庫集合更新後更新表

上的數據變化:

$scope.changeStatus = function($event,label,status){ 
    var target = $event.currentTarget; 
    target = $(target).parents('tr').attr('id'); 
    $http({ 
     method: 'POST', 
     url: '/update', 
     params: { 
      trgt : target, 
      label : label, 
      labelstatus : status, 
      searchQuery : $scope.search 
     } 
    }) 
    .success(function(data){ 
     console.log(data); 
     $scope.events = data; 
    }) 
    .error(
    function(error){ 
      console.log(error) 
    }); 

} 

然後:

app.post('/update', function(req,res){ 
    ImportCollection.findOne({ _id : req.query.trgt },function(err,doc){ 
     doc.label.label = req.query.labelname; 
     doc.label.status = req.query.labelstatus; 
     doc.save(); 
}); 

//到達這一切都運行得很好

if(req.query.searchQuery){ 
ImportCollection.find({$or:[ 
    {'localizedData.0.title' : {'$regex': req.query.searchQuery, $options: 'i' }}, 
    {'licensor.name' : {'$regex': req.query.searchQuery, $options: 'i'}} 
    ]}) 
    .exec(function(err, imports){ 
     if(err) throw err 
     res.json(imports) 
     }) 
    } else{ 
     ImportCollection.find({},function(err, imports){ 
      if(err) throw err 
      res.json(imports) 
      }) 

    } 
}); 

但隨後應更新表數據的響應,是背後的一個要求。 因此當前的Data = Live,我將它設置爲QA,沒有任何反應。該表仍然顯示Live。一旦我現在改變它,讓我們說拒絕,表格顯示質量保證。我希望現在更清楚。

有沒有人有想法?

回答

0

傳遞查找()塊作爲一個回調函數到doc.save()方法爲我工作:

doc.save(function(err){ 
      if (err) throw error 

      var query = req.query.searchQuery; 
      if(query) { 
       ImportCollection.find({$or:[ 
        {'localizedData.0.title' : {'$regex': req.query.searchQuery, $options: 'i' }}, 
        {'licensor.name' : {'$regex': req.query.searchQuery, $options: 'i'}} 
        ]}).exec(function(err, imports){ 
        if(err) throw err 
        res.json(imports) 
       }); 
      } else{ 
       ImportCollection.find({}).exec(function(err, imports){ 
        if(err) throw err 
        res.json(imports) 
       }); 
      } 


     });