尝试在ScriptDb中存储数组时出错 [英] Error when trying to store an array in ScriptDb

查看:98
本文介绍了尝试在ScriptDb中存储数组时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由我的脚本创建的对象数组,我试图将该数组复制到一个新数组中,然后使用以下函数将其存储在scriptDb中:

I have an array of objects that is created by my script and I am trying to copy that array into a new array and then store it in scriptDb using the following function:

function copyAndStore (currentArray) {
  var db = ScriptDb.getMyDb();
  var copyArray = [];
  for (var i in currentArray) {
    copyArray.push(currentArray[i]);
  }
  var id = db.save(copyArray);
  return id;
}

它正确复制所有内容,但是当它到达 var id = db.save(copyArray); 我得到错误:无效的参数。预计有一个JavaScript地图对象。

It copies everything properly but when it gets to var id = db.save(copyArray); I get the error: Invalid argument. Expected a javascript map object.

ScriptDb在存储数组方面有问题吗?感谢您的帮助。

Does ScriptDb have issues with storing arrays? Thanks in advance for the help.

推荐答案

在将对象放入ScriptDB之前,您不需要执行复制操作。您可以通过 db.save({myArray})来保存数组,并记住该ID。

You don't need to perform a copy operation before putting an object into the ScriptDB, either. You could save your array by simply db.save({myArray}), and remember the ID.

这里有一些简单的代码可供演示。我展示了两种方式来检索您保存的数组 - 一个是ID,这似乎是您计划的方式,另一个方法是使用键值查询。如果您希望在稍后的代码中检索ScriptDB的内容,则此方法不需要记住存储数组的ID。

Here's some minimalist code to demonstrate. I'm showing two ways to retrieve your saved array - one by ID, which seems to be the way you were planning to, but also a second way using a "key" value for a query. If you expect to retrieve the contents of ScriptDB in a later run of your code, this approach eliminates the need to somehow remember the ID of the stored array.

function saveArray (currentArray) {
  var db = ScriptDb.getMyDb();
  return db.save({type: "savedArray", data:currentArray}).getId();
}

function loadArrayById (id) {
  var db = ScriptDb.getMyDb();
  return db.load(id).data;
}

function loadArrayByType () {
  var db = ScriptDb.getMyDb();
  var result = db.query({type: "savedArray"});
  if (result.hasNext()) {
    return result.next().data;
  }
  else {
    return [];
  }
}

function test() {
  var arr = ['this','is','a','test'];
  var savedId = saveArray( arr );
  var loaded1 = loadArrayById( savedId );
  var loaded2 = loadArrayByType();
  debugger;  // pause if running debugger
}

以下是您将在调试器暂停时看到的内容:

Here's what you'll see at the debugger pause:

请注意,通过使用地图标记 data 从已保存的对象中提取数组, loaded1 loaded2 与源数组 arr 相同。

Note that by using the map tag data to pull the array from the saved object, both loaded1 and loaded2 are identical to the source array arr.

这篇关于尝试在ScriptDb中存储数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆