How to get the data of a query in arangojs

511 views Asked by At

Hello I'm using arangojs, created a database, added data to the collection beusers.

Now I want to read the added data. In ArangoDB I can do so using the query

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

Doing

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

in arangojs I get

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

The example in the readme got me only so far.

What am I missing? How can I read the data?

2

There are 2 answers

0
gurisko On BEST ANSWER

You want to use 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()).

So the code would look like this:

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
0
pgalle On

It seems I just used the wrong method there. Correct would've been:

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];
      }
    `,
  )