You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Arangojs从ArangoDB取数据时遇TypeError: db.database is not a function错误

Fixing "TypeError: db.database is not a function" in ArangoJS

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:34:18