Google Cloud SQL not updating with script

问题: I have a long script which is designed to edit a specific row in the Cloud SQL table. The code is long so i will shorten it. Client Side: function build_profile(){ var...

问题:

I have a long script which is designed to edit a specific row in the Cloud SQL table. The code is long so i will shorten it.

Client Side:

function build_profile(){
var cbid = sessionStorage.getItem("client_id");
var self = this;
var createSuccess = function(data){
  var statuse = ["Active", "Wiating", "Discharged"];
  if(data !== false){
    data = data.split(",");
    var dec = app.pages.Profile.descendants;
    dec.fname.text = data[1];
    dec.sname.text = data[3];
    sessionStorage.setItem("school_id", data[9]);
    app.popups.Loading.visible = false;
  }
  };
var init = function() {google.script.run.withSuccessHandler(createSuccess).get_user_data(cbid);};
app.popups.Loading.visible = true;
init();
}

function save_profile() {
var createSuccess = function(data){
var dec = app.pages.Profile.descendants;
console.log(data);
if(data !== -1){
  var ds = app.datasources.Clients;
  ds.load(function(){
    ds.selectIndex(data);
    console.log("editing:"+ds.item.CBID);
    ds.item.fname = dec.fname_edit.value;
    ds.item.sname = dec.sname_edit.value;
    ds.load(function(){build_profile();});
  });
}
}};
var init = function() {google.script.run.withSuccessHandler(createSuccess).update_client(sessionStorage.getItem("client_id"));};
init();
}

Server Side:

function get_user_data(cbid){
try{
var query = app.models.Clients.newQuery();
query.filters.CBID._equals = parseInt(cbid);
var results = query.run();
if(results.length > 0){
  var arr = [
    results[0].Id, //0
    results[0].fname, //1
    results[0].sname //3
      ];
    return arr.join(",");
  }else{
    return false;
  }
  }catch(e){
  console.error(e);
  console.log("function get_user_data");
  return false;
  }
}
function update_client(cbid) {
  try{
    var ds = app.models.Clients;
    var query = ds.newQuery();
    query.filters.CBID._equals = parseInt(cbid);
    var results = query.run();
    if(results.length > 0){
      var id = results[0]._key;
      return id+1;
    }else{
      return -1;
    }
  }catch(e){
    console.error(e);
    return -1;
  }
}

This gets the Clients table and updates the row for the selected client, then rebuilds the profile with the new information.

EDIT: I have managed to get to a point where its telling me that i cannot run the query (ds.load()) while processing its results. There does not seem to be a manual check to see if it has processed?

Note: datasource.saveChanges() does not work as it saves automatically.


回答1:

You error is being produced by the client side function save_profile() and it is exactly in this block:

  ds.load(function(){
    ds.selectIndex(data);
    console.log("editing:"+ds.item.CBID);
    ds.item.fname = dec.fname_edit.value;
    ds.item.sname = dec.sname_edit.value;
    ds.load(function(){build_profile();});
  });

So what you are doing is reloading the datasource almost immediately before it finishes loading hence you are getting that error

cannot run the query (ds.load()) while processing its results

This is just a matter of timing. A setTimeout can take of the issue. Just do the following:

ds.load(function(){
  ds.selectIndex(data);
  console.log("editing:"+ds.item.CBID);
  ds.item.fname = dec.fname_edit.value;
  ds.item.sname = dec.sname_edit.value;
  setTimeout(function(){
      ds.load(function(){build_profile();});
  },1000);
});
  • 发表于 2019-03-19 15:40
  • 阅读 ( 187 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除