查询到mySQL并不总是调用最新数据

时间:2018-10-12 11:07:54

标签: jquery mysql node.js

确实搜索了此答案,但也许我所说的方式不正确。我正在使用节点和mySQL。我有一个客户端屏幕。我单击“编辑/查看”按钮。

start

出现编辑屏幕。

edit screen

然后我编辑客户(在这种情况下为名字)并提交。

edit first name

提交后,我看到主要客户ul上的更改,在那里我可以看到名字已更改。

after edit

当我再次按下“编辑/查看”按钮时,我看不到我的更改,但是更改肯定在数据库中。

re enter edit

如果我导航到一个完全不同的视图,则可以看到更改已生效。

same client different view

如果我导航回到客户端视图并按“编辑/视图”按钮,则该按钮不会显示。

return to edit client

我无法理解我做错了什么。它可以在我的本地主机和本地数据库上正常工作。

我的路线。

var clientReturned;
router.get('/getclient/:clientID', function(req, res){
  client = req.params.clientID;
  var sql = `select cl.id, cl.first_name, cl.last_name, 
             cl.email, cl.client_type, ct.client_type_description, 
             cl.active, cl.activity_reason from clients cl 
             inner join client_type  ct on ct.id = cl.client_type
             where cl.id = ${client}`;
  connection.query(sql, function (error, results, fields) {
    if (error) throw error;
    clientReturned = results;
    console.log(clientReturned);
    res.send({ clientReturned: clientReturned }); 
  });
});

router.post('/editClient', function(req, res){
  var client = {
      last_name: req.body.lastName,
      first_name: req.body.firstName,
      email: req.body.email,
      active: req.body.activity,
      activity_reason: req.body.activityReason,
      client_type: req.body.clientType
  } 
  var id = req.body.id;
  console.log(client);
  connection.query(`UPDATE clients SET ? where ID = ${id}`,client, function (error, result) {
    if (error) throw error;
    res.redirect('/clients');  
 });
});

客户端:

var clientID;

$('#clientData').on("click",".edit-btn",function(e){
    clientID = $(this).attr('id');
        e.preventDefault();
        var x = (screen.width);
        var y = (screen.height)
        var swidth = (screen.width) * .6; 
        var sheight = (screen.height) * .6;
        var newTop = (y - sheight)/2;
        var newLeft = (x - swidth)/2;
        var newMargin = (swidth - 300)/2;
        $(".lightbox1").fadeIn().css("width", x).css("height", y);
        $(".lightboxA1").css("width",swidth).css("height", sheight).css("top", newTop).css("left", newLeft);
        $('.lightboxA1').fadeIn().css("display", "flex").css("align-items", "center").css("align-content", "space-around");
        $(".inputData1").css("margin-left", newMargin);
        editData = [];//clears edit query
        clientType = [];
        clearClientType();
        $('.inputDataFields1 > select').css({"color": "red"});
        $('#editClientType').empty();
        var url = '/getclient/' + clientID;
        var url2 = '/getClientTypes';
        $.get(url, function(data){  
            editData = data.clientReturned;   
        }).then(function(){
            $.get(url2, function(data){  
                clientTypes = data;   
            }).then(function(){
                fillEditInputs();
                console.log("Success");
            }).catch(function(){
                console.log("Some Issue Here");
            });
        });       
    });


var editData = [];//clears edit query
var clientTypes = [];//clears clientType

function clearClientType() {
    $('#editClientType').empty();
}


function fillEditInputs() {  
    clientTypes.forEach(function(clientType){
            val = clientType.id
            const text = clientType.client_type_description;
            $(`<option value="${val}">${text}</option>`).appendTo('#editClientType');
    });

    $("#client-id").val(editData[0].id);
    $("#editLastName").val(editData[0].last_name);
    $("#editFirstName").val(editData[0].first_name);
    $("#editEmail").val(editData[0].email);
    $('#editClientType').val(editData[0].client_type);
    if (editData[0].active === 1) {
    $("#activeChecked1").prop("checked", true);
    $("#isActive").val(1)
    $("#activeChecked1").val(1);
    $("#inactivityReason").css("display", "none");
    $("#inactivityReasonLabel").css("display", "none"); 
    } else {
    $("#activeChecked1").prop("checked", false);
    $("#activeChecked1").val(0);
    $("#isActive").val(0)
    $("#inactivityReason").val(editData[0].activity_reason);
    }

}

0 个答案:

没有答案