使用Arangojs从ArangoDB取数据时遇TypeError: db.database is not a function错误
Hey there, let's get this error sorted out for you! The issue here boils down to a breaking API change between older and newer versions of the arangojs library.
What's going wrong?
If you're using arangojs v3 or later, the database() method no longer exists. It was replaced with useDatabase() to make the API more intuitive. Alternatively, you can specify your target database right when you initialize the connection, which is even cleaner.
Solution 1: Specify the database during initialization
This is the simplest approach—just tell arangojs which database to connect to upfront:
var Database = require('arangojs'); // Initialize the connection with your target database included var db = new Database({ url: 'http://127.0.0.1:8529', databaseName: 'superrango' }); module.exports = { getAllUsers: function() { return db.query('FOR x IN Users RETURN x') .then(function(cursor) { return cursor.all(); }); } };
Solution 2: Use useDatabase() to switch databases
If you need to switch databases dynamically after initialization, replace the old database() call with useDatabase():
var Database = require('arangojs'); var db = new Database({url:'http://127.0.0.1:8529'}); module.exports = { getAllUsers: function() { return db.useDatabase('superrango') // Replace database() with useDatabase() .then(function() { return db.query('FOR x IN Users RETURN x'); }) .then(function(cursor) { return cursor.all(); }); } };
Quick note for older arangojs versions
If you're stuck using arangojs v2 or earlier, the database() method does exist—but I'd strongly recommend upgrading to the latest version for better security and features. If you can't upgrade, double-check that your package.json specifies the correct older version of arangojs.
内容的提问来源于stack exchange,提问作者user3531208

