在 Waterline 模型中存储和解析数组

Store and parse array in model in Waterline

使用最新版本的 Waterline 0.13.1-6 独立。

array 类型不再存在于此版本中。所以我假设现在存储数组的方式是使用 JSON 类型。

我的模型样本 Model:

  attributes: {
    someArray: { type: 'json' }
  }

问题:在 Model 的实例上,model.someArray 现在是一个字符串。每次我请求一个获取数组中的值时,我都应该 JSON.parse 它。这很不方便,显然会导致错误。

在新的 Waterline 中是否有内置的方法来清理它(自动解析 JSON 字段...)?

您可以按照您的建议使用 JSON。无需解析它,这是在您执行元提取或查找时自动完成的。你可以做到

YourModel.create({someArray: [1,2,3]}).meta({fetch: true}).then( out => { console.log(out.someArray[0]); //1; });

我会有一些其他的识别属性来找到它,比如 myRef: {type: 'string'}

那么你可以

YourModel.find({myRef: 'something'}).limit(1).then( out => { console.log(out[0].someArray[1]); //2 });