3

I want to execute a simple query in my database arangodb with nodejs for a login page.

var arango = require('arangojs');
/*---*/
Database = arango.Database;  
db = new Database('http://127.0.0.1:8529');
db.useDatabase('MyDB');
db.useBasicAuth('root', 'root');
/*---*/

//Post Methods
var username;
var passworddb;

//Login method
app.post('/', function (request, response) {

    username = request.body.user.name;

    db.query('FOR u IN User FILTER u.firstname == "'+username+'" RETURN u.password').then(
      cursor => cursor.all()
    ).then(
      key => console.log('key:', key),
      //key => passworddb = key,
      err => console.error('Failed to execute query')
    );

    console.log(passworddb)
});

It's working, but i can't save in my var passworddb the result of the key... I don't undertand... the request is asynchronous in arangodb

Console.write(passworddb) return "undefined" and after console.log('key:', key) return correctly the key... Twice.

My question is : how can i get only the password in arangodb (What is the query for once request) and how to store the value in my var ?

Thank you for your answers.

2 Answers 2

2
// required package for arango
const arangojs = require('arangojs');

   //sample function
   async function main(){
       DB = new arangojs.Database({ url: <arango_url> });
       DB.useDatabase(<arango_db_name>);
       DB.useBasicAuth(<arango_db_username>, <arango_db_password>);
       const data = await DB.query('RETURN LENGTH(<arango_collection_anme>)')
       console.log("result", data._result)
   }
   main()
Sign up to request clarification or add additional context in comments.

Comments

1

The line console.log(passworddb) gets executed before //key => passworddb = key happens because of the asynchronous request. So by the time you try to log the password, the db response has not arrived yet. The password gets saved correctly in passworddb, you just console.log too soon. You could do

key => {
  passworddb = key;
  console.log(passworddb);
}

to confirm that the assignment works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.