查询引用其他数据源的数据源

时间:2018-10-12 09:10:25

标签: google-app-maker

我不确定在这里是否采用正确的方法;我有两个数据模型,CoursesFiled和TraineesFiled。它们都有一个名为CourseName的字段。每个受训者记录将具有一个CourseName字段,该字段也将显示在CoursesFiled中。

最终产品必须为两页。首先,有一张包含所有课程的表格。选中一个,然后将用户带到下一页,该表将显示所选课程中的所有受训者。

对此我有些陌生,认为最简单的方法是在TraineesFiled模型上建立查询数据源,并使用如下查询脚本:

var courseName = "Register-2012-10-02(xxxxxxxxx).xml";//hardcoded for now, to be replaced with variable being passed from UI for app.datasources.CoursesFiled.item.CourseName;
var query = app.models.TraineesFiled.newQuery();
query.filters.CourseName._equals = courseName ;
query.run();

当我使用query-datasouce作为数据源创建新表时,它会加载以下错误:

Fri Oct 12 10:59:46 GMT+200 2018
The function queryRecords returned undefined, please make sure a value was 
explicitly returned. Error: The function queryRecords returned undefined, 
please make sure a value was explicitly returned.
E
Fri Oct 12 10:59:46 GMT+200 2018
Executing query for datasource TraineesFiled1: (Error) : The function 
queryRecords returned undefined, please make sure a value was explicitly 
returned.
E
Fri Oct 12 10:59:46 GMT+200 2018
Executing query for datasource TraineesFiled1 failed.

我要去哪里错了?我是否完全以错误的方式解决了这个问题?

1 个答案:

答案 0 :(得分:1)

official documentation 中所述,使用查询脚本时,您需要 RETURN 结果。

  

例如,假设您只想返回年龄为偶数的雇员,并应用查询中指定的最小年龄。您可以在查询脚本代码编辑器中编写以下代码片段:

var employeeResult = [];
// Modify passed query to additionally filter by minimum age.
query.filters.Age._greaterThan = 20;
var employeesAboveMinimumAge = query.run();
for (var i = 0; i < employeesAboveMinimumAge.length; i++) {
  if (employeesAboveMinimumAge[i].age % 2 == 0) {
    employeeResult.push(employeesAboveMinimumAge[i]);
  }
}
return employeeResult;

这意味着您要做的就是在代码中添加 return 语句:

var courseName = "Register-2012-10-02(xxxxxxxxx).xml";
var query = app.models.TraineesFiled.newQuery();
query.filters.CourseName._equals = courseName ;
return query.run();