【问题标题】:MEAN App - Updating table after DB Collection updateMEAN App - 数据库集合更新后更新表
【发布时间】:2016-09-22 13:54:41
【问题描述】:

我正在尝试使我的表数据与数据库数据保持同步。

关于数据更改:

    $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)
            })

    }
});

但是应该更新表数据的响应总是落后一个请求。 所以当前数据 = 实时,我将其设置为 QA,但没有任何反应。该表仍显示实时。一旦我现在更改它,让我们对 DENIED 说,表格显示 QA。我希望现在更清楚了。

有人有想法吗?

【问题讨论】:

    标签: javascript angularjs rest express mean-stack


    【解决方案1】:

    将 find() 块作为回调函数传递给 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)
                  });
              }
    
    
          });
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多