如何在arangojs中获取查询的数据

How to get the data of a query in arangojs

你好,我正在使用 arangojs,创建了一个数据库,向集合中添加了数据 beusers

现在我想读取添加的数据。在 ArangoDB 中,我可以使用查询

FOR user IN beusers
FILTER user.password == '3670747394' && user.email == '3817128089'
RETURN user

正在做

const user = await usedDB.parse(aql`
        FOR user IN ${usersCol.name}
        FILTER user.password == ${hashedPassword} && user.email == ${hashedEmail}
        RETURN user
`)
console.log(user)

在arangojs中我得到

{
  error: false,
  code: 200,
  parsed: true,
  collections: [],
  bindVars: [ 'value2', 'value0', 'value1' ],
  ast: [ { type: 'root', subNodes: [Array] } ]
}

readme 中的示例只让我了解了这么多。

我错过了什么?如何读取数据?

看来我只是用错了方法。正确的应该是:

const user = await db.executeTransaction(
    {
      read: ['beusers'],
    },
    `
      function() {
        // This code will be executed inside ArangoDB!
        const { query } = require("@arangodb");
        return query\`
            FOR user IN ${col.name}
            FILTER user.password == "${stringHash(
              passedUser.password,
            )}" && user.email == "${stringHash(passedUser.email)}"
            RETURN user
          \`.toArray()[0];
      }
    `,
  )

您想使用 Database.query() to get a cursor (ArrayCursor) and then you want to return the value in the cursor's result list (for example using ArrayCursor.all()).

所以代码看起来像这样:

const cursor = await usedDB.query(aql`
  FOR user IN ${usersCol.name}
    FILTER user.password == ${hashedPassword} && user.email == ${hashedEmail}
    RETURN user
`);
console.log(await cursor.all()); // returns array of users