简体   繁体   中英

How to get the data of a query in arangojs

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM