0

I am trying to use ArangoJS drive on Node JS, when I add following code to main app.js file it works but when added to a function in a class in different file it throws an error.

Database is not defined

Code is as follows

class User {
insertUser() {
        Database = require('arangojs').Database;
        db = new Database();
        db.useBasicAuth("", "");
        db.useDatabase('_system');

        collection = db.collection('Users');
        doc = {
          _key: 'firstDocument',
          a: 'foo',
          b: 'bar',
          c: Date()
        };
        collection.save(doc).then(
          meta => console.log('Document saved:', meta._rev),
          err => console.error('Failed to save document:', err)
        );
}
contructor() {

}
}



 module.exports = User;

If I use the code from insertUser function in app.js directly it works fine.

Kindly advice

Thanks

Note: I am using Express with Node js

1 Answer 1

1

You need to require database outside of the class and use const:

const { Database } = require('arangojs');
class User {
contructor() {}

insertUser() {
        db = new Database();
        db.useBasicAuth("", "");
        db.useDatabase('_system');

        collection = db.collection('Users');
        doc = {
          _key: 'firstDocument',
          a: 'foo',
          b: 'bar',
          c: Date()
        };
        collection.save(doc).then(
          meta => console.log('Document saved:', meta._rev),
          err => console.error('Failed to save document:', err)
        );
}

}



 module.exports = User;
Sign up to request clarification or add additional context in comments.

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.