javascript 如何从scriptDB中查询数组值?

3lxsmp7m  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(104)

我尝试使用scriptDb从主函数到处理函数访问全局变量。按照[documentation instructions][1],我创建了一段类似如下的代码:

function showList(arrayList) {
   Logger.log("arrayList argument value = " + arrayList);
      var arrayToUse = {
        arrayName: 'theses',
        arrayValue: arrayList,
    };
    var record = db.save(arrayToUse);
    var scriptDbObject = db.query({arrayName: 'theses'});
    var arrayList = scriptDbObject.arrayValue;
    Logger.log("ShowList arrayList query value = " + arrayList);
    }

但当我尝试查询插入的项目时,它不起作用:
![在此输入图像说明][2]
参数arrayList正常(如日志所示)。
我还尝试了其他代码,尽可能地遵循类似的代码[这个stackoverflow问题][3]:

function showList(arrayList) {
  Logger.log("arrayList argument value = " + arrayList);
  var record = db.save({arrayName: "savedArray", arrayValue: arrayList});
  var result = db.query({arrayName: 'thesesRequests'});
  if (result.hasNext()) {
    arrayList = result.next().data;
    }
  else {
    arrayList ["empty array", "empty", "array"];
  }
    Logger.log("ShowList arrayList query value = " + arrayList);

我也收到了同样的消息。
这里出了什么问题?[1]:https://developers.google.com/apps-script/guides/script-db/ [2]:http://i.stack.imgur.com/5CpyY.png [3]:Error when trying to store an array in ScriptDb

dohp0rv5

dohp0rv51#

您的第一次尝试是正确的,只是当您执行查询时,需要对结果使用next()操作符以获得实际的dbResult条目:

function showList(arrayList) {
   Logger.log("arrayList argument value = " + arrayList);
      var arrayToUse = {
        arrayName: 'theses',
        arrayValue: arrayList,
    };
    var record = db.save(arrayToUse);
    var scriptDbObject = db.query({arrayName: 'theses'});
    if (scriptDbObject.hasNext()) {
      var dbItem = scriptDbObject.next();
      var dbArrayList = dbItem.arrayValue 
      Logger.log("ShowList arrayList query value = " + dbArrayList);
    }
 }

相关问题