对象数组数据类型

Object Array data type

此 AQL return 是我想要使用的对象。

const keys = db._query(aql`
    FOR huis IN test
    FILTER huis._key in ${req.queryParams.keys}
    RETURN {
        'key': huis._key,
        'adres': huis.adres,
        'postcode': huis.postcode,
        'plaats': huis.plaats
    }
`);

这个 return 这个对象:

[
  {
    "key": "374875",
    "adres": "Klaverstraat 7",
    "postcode": "2197GV",
    "plaats": "Leiden"
  }
]

那我想这样取钥匙:

keys[0].key

这在我制作 fiddle 时在 JavaScript 中有效,但在 Foxx 中无效。

const test = [
  {
    "key": "374875",
    "adres": "Klaverstraat 7",
    "postcode": "2197GV",
    "plaats": "Leiden"
  }
]

console.log(test[0].key)

374875

为什么在 Foxx 中 return 'undefined' 而 Fiddle 中的数据正确?

这个 Foxx 代码:

router.get('/', function (req, res) {
  const test = [
    {
      "key": "374875",
      "adres": "Klaverstraat 7",
      "postcode": "2197GV",
      "plaats": "Leiden"
    }
  ];

  console.log(test[0].key);
  res.status(200).send(test[0]);
}, 'test1')
  .summary('test1')
  .description('test1');

Returns 通过 REST:

{
  "key": "374875",
  "adres": "Klaverstraat 7",
  "postcode": "2197GV",
  "plaats": "Leiden"
}

登录 ArangoDB 日志:

374875

当您通过查询获取数据时,您需要确保在读取结果内容之前完成查询。

看这段代码:

router.get('/', function (req, res) {
  const keys = db._query(aql`
    FOR huis IN test
    FILTER huis._key == "374875"
    RETURN {
        'key': huis._key,
        'adres': huis.adres,
        'postcode': huis.postcode,
        'plaats': huis.plaats
    }
`).toArray();

  console.log(keys[0].key);

  res.status(200).send(keys);

}, 'test1')
  .summary('test1')
  .description('test1');

这里改变了FILTER条件,在查询执行后添加了.toArray()。这确保查询完成,然后 keys 的内容如您所料。