readRecords(table, terms, callback) 读取表数据

功能分类: Data Applab JavaScript 少儿编程

在 Applab 表数据存储里,在指定名称的 table 表里,读取与指定的 terms 匹配的记录,并指定操作结束后异步调用的 callback 回调函数。 读取返回结果将作为参数传递给回调函数。

为 APP 应用增加持久数据存储可以让你编写的程序成为一个真正的 APP 应用 - 像你平时使用的 APP 一样。我们使用的很多应用程序都是通过 “云端” 数据驱动的。通过使用这里的方法,你也可以开发一个 “云端” 数据驱动的应用。

第一次使用 Applab 表数据存储? 在这里 表数据存储 查看 Applab 表数据存储介绍以及使用方法。

传递的 terms 参数必须是 javaScript object 对象变量或使用 {}: 定义的 javaScript object 对象。 terms 被用于查询跟其描述的数据列完全匹配的数据。一个 APP 应用只能访问在应用自己里面创建的数据。

要查看 APP 里的表数据,在 Applab 页面点击并打开 数据 页面,在页面里会显示 APP 相关的所有数据表信息,点击数据表即可查看或修改表数据。

示例代码



textInput("nameInput", "What is your name?");
textInput("ageInput", "What is your age?");
textInput("foodInput", "What is your favorite food?");
button("submitButton", "Submit");
button("displayButton", "Display");

onEvent("submitButton", "click", function() {
  var favFoodData={};
  favFoodData.name = getText("nameInput");
  favFoodData.age = getNumber("ageInput");
  favFoodData.food = getText("foodInput");
  createRecord("fav_foods", favFoodData, function(record) {
    console.log("Record created with id:" + record.id);
    console.log("Name:" + record.name + " Age:" + record.age + " Food:" + record.food);
  });
});

onEvent("displayButton", "click", function() {
    readRecords("fav_foods", {}, function(records) {
        if (records.length>0) {
            for (var i =0; i < records.length; i++) {
              write("id: " + records[i].id + " Age:" + records[i].age + " Food: " + records[i].food);
            }
        }
        else {
              write("No records to read");
        }      
    });
});


示例代码:筛选数据 限定条件读取数据,只读取 driverAge=16 的数据。


// 限定条件读取数据,只读取 driverAge=16 的数据。
textInput("nameInput", "What is your name?");
textInput("ageInput", "What is your age?");
textInput("foodInput", "What is your favorite food?");
button("submitButton", "Submit");
button("displayButton", "Display New Drivers Only");

onEvent("submitButton", "click", function() {
  var favFoodData={};
  favFoodData.name = getText("nameInput");
  favFoodData.age = getNumber("ageInput");
  favFoodData.food = getText("foodInput");
  createRecord("fav_foods", favFoodData, function(record) {
    console.log("Record created with id:" + record.id);
    console.log("Name:" + record.name + " Age:" + record.age + " Food:" + record.food);
  });
});

onEvent("displayButton", "click", function() {
    var driverAge=16;  
    readRecords("fav_foods", {age:driverAge}, function(records) {
        if (records.length>0) {
            for (var i =0; i < records.length; i++) {
              write("id: " + records[i].id + " Age:" + records[i].age + " Food: " + records[i].food);
            }
        }
        else {
              write("No records to read");
        }      
    });
});

示例代码:最后一行 只读取表数据中最后一行的数据。


// 只读取表数据中最后一行的数据。
textInput("nameInput", "What is your name?");
textInput("ageInput", "What is your age?");
textInput("foodInput", "What is your favorite food?");
button("submitButton", "Submit");
button("displayButton", "Display Last In Line");

onEvent("submitButton", "click", function() {
  var favFoodData={};
  favFoodData.name = getText("nameInput");
  favFoodData.age = getNumber("ageInput");
  favFoodData.food = getText("foodInput");
  createRecord("fav_foods", favFoodData, function(record) {
    console.log("Record created with id:" + record.id);
    console.log("Name:" + record.name + " Age:" + record.age + " Food:" + record.food);
  });
});

onEvent("displayButton", "click", function() {
    readRecords("fav_foods", {}, function(records) {
        if (records.length>0) {
            var last=records.length-1;
            write("id: " + records[last].id + " Age:" + records[last].age + " Food: " + records[last].food);
        }
        else {
              write("No record to read");
        }      
    });
});

语法规则


readRecords(table, terms, function(records){
    // 在这里编写回调函数代码
  });

参数说明

名称 类型 必需 参数描述
table string Yes 指定表存储中的表名称。
terms object Yes 指定表数据查询对象。必须是 javaScript object 对象变量或使用 {}: 定义的 javaScript object 对象。(参考下面代码。)
callback function Yes readRecords 调用完成后被异步调用执行的函数。一个包含 array 数组类型的数据结果参数将被传递给回调函数。

返回值

没有返回值。但在 readRecords() 成功执行并读取数据后,读取到的数据(数组类型)会作为参数传递给 callback 回调函数,同时异步调用并执行回调函数。

提示说明

  • The JavaScript object properties in term must match the App Lab table column names. Both are case sensitive.
  • readRecords() 有一个异步 callback 回调函数,是因为它需要访问远程数据存储,并不会立刻获得返回结果。通过使用异步回调函数,主代码无需等待。请求结果返回后在这个回调函数里异步处理。
  • callback 回调函数可以是一个内联函数,也可以是在应用程序中单独定义并被 readRecords() 调用的函数。
  • 通常不要将包含异步调用的函数如:readRecords() ,放在循环里执行。因为循环里不会等异步调用结束才继续执行。
  • 通常与这些方法:createRecord()deleteRecord() 以及 updateRecord() 实现对表数据记录的查看、删除以及更新。

查看更多少儿编程教程、JavaScript 介绍

返回文档首页