Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud sql 作为小部件数据源查询_Google Cloud Sql_Google App Maker - Fatal编程技术网

Google cloud sql 作为小部件数据源查询

Google cloud sql 作为小部件数据源查询,google-cloud-sql,google-app-maker,Google Cloud Sql,Google App Maker,我试图将小部件的数据源定义为查询的结果,但我不确定它是否可行 我正在使用SQL视图和一个表,我想显示我在表上的ID值,这些ID来自视图 function queryValue(source, model, key){ console.log("source " + source); app.datasources[model].query.filters.id._equals = source; app.datasources[model].load(function () {

我试图将小部件的数据源定义为查询的结果,但我不确定它是否可行

我正在使用SQL视图和一个表,我想显示我在表上的ID值,这些ID来自视图

function queryValue(source, model, key){
  console.log("source " + source);
  app.datasources[model].query.filters.id._equals = source;
  app.datasources[model].load(function () {
    console.log(app.datasources.users.items[0][key]);
    return app.datasources.users.items[0][key];
  });
  app.datasources[model].query.clearFilters();
}
称之为:

queryValue(@datasource.item.[the_id], "[the_SQLView_Datasouce]", "[the_field_i_want]");
在本例中,小部件是一个表,因此函数将重复talbe中的项目数量

问题是,要么我得到的结果与物品数量的meny倍相同,要么第一个不起作用

第二个问题是,结果没有重写要显示的小部件文本。


这是一个非常简单的函数,我确实找到了一些解决方法,但没有使用datasource功能,而且它们工作得太慢,有什么建议吗?是否可以对数据源执行此操作?

如果我正确理解此问题,您可能希望在服务器端执行查询。发布的示例代码的问题是,在任何加载返回之前,它会多次触发单个数据源上的加载。完成此操作后,数据源只加载其中一个加载的结果,我相信是最后一个加载的结果。因此,您可能看到了上次查询所有回调的结果

因此,您的代码应该是服务器端脚本,应该类似于:

function queryValue(source, model, key){
  console.log("source " + source);
  var query = app.models.newQuery();
  query.filters.id._equals = source;
  var results = query.run;
  return results[0].key;
}

(从内存中写入,请原谅任何错误。)

遵循Devin的建议:

前端

/*****************************************************************************
Front-end function that calls the querying function @queryValue(source, model, key) in controller_TransformId
@source => the field ID to transform to label
@model => the model name to be queried
@key => the label to be acquired with the query
@wwidget => the widget making the request
This function works as a model to manage the transactions between the 
controller at the backend and the view.  
******************************************************************************/
function buildTransformID(source, model, key, widget){ 
  google.script.run.withSuccessHandler(
    function successHandler(expectedValue){
      widget.text = expectedValue;})
  .withFailureHandler(
    function failureHandler(){
      widget.text = "undefined";})
  .queryValue(source, model, key);
}
后端

/*****************************************************************************
Back-end function that queries the database
@source => the field ID to transform to label
@model => the model name to be queried
@key => the label to be acquired with the query    
This function works works as a controller to query the database from the backend    ******************************************************************************/
function queryValue(source, model, key){ 
  var query = app.models[model].newQuery();
  query.filters.id._equals = source;
  var results = query.run();
  console.log("CONTROLLER return :" + results[0][key]);
  return results[0][key];
}

是否必须传递widget.text值?successHandler回调是异步的,因此常规返回只会从屏幕截图中给我空值,看起来您正试图显示关系的ID。如果您有两个模型(表),比如Employee和Manager,或者您有自引用的Employee模型(表),那么您可以为数据源添加预回迁。在这种情况下,您可以像这样绑定Manager Id:@datasource.item.Manager.Id。Docs:因为它是一个SQL视图,所以没有关系,所以我必须手动查询=),在这种情况下,如果表是仅视图,我建议使用计算数据源。这将减少对服务器和数据库的调用数量,从而提高页面的整体性能,并消除标签闪烁。在当前的实现中,对于包含N行的表,应用程序将至少进行(N+1次服务器调用)+(N+1次数据库调用)。使用计算数据源,您将其减少为1+1=2次调用。@如何导入sql视图?我知道我可以使用cloud shell创建视图。但是,我在appmaker中看不到导入它的选项。你是怎么做到的?再次感谢Devin!经过一些小的Tweek之后,这个解决方案就成功了!