简体   繁体   English

Node.js + Arangodb建立连接并获取查询

[英]Node.js + Arangodb to make a connection and get a query

I am new to backend service. 我是后台服务的新手。 I have been trying to make a connection to arangodb from node server. 我一直在尝试从节点服务器连接到arangodb。 I have a created a basic query in arangodb. 我在arangodb中创建了一个基本查询。 the user auth in arangodb is so complicated and there are very few tutorials for node and arangadb combo. arangodb中的用户身份验证非常复杂,节点和arangadb组合的教程很少。 I have posted my code below. 我在下面发布了我的代码。 I have found this example online. 我在网上找到了这个例子。

DataService.js

var arangojs = require('arangojs');

// Const variables for connecting to ArangoDB database

const host = '192.100.00.000'
const port = '8529'
const username = 'abcde'
const password = 'abcde'
const path = '/_db/_system/_api/'
const databasename = 'xyzm_app'

// Connection to ArangoDB
var db = new arangojs.Database({
url: `http://${host}:${port}${path}${databasename}`,
databaseName: databasename
});

 db.useBasicAuth(username, password);
//console.log(db);

module.exports = {
getAllControllers : function(){
     return db.query('For x IN {user_details} RETURN NEW {
id: x._user_details/7977010,
name: x.char_ctrl_username,
email:x.char_ctrl_email
}')
    .then(cursor => {
        return cursor.all()

 });
}

query in db 在db中查询

在此输入图像描述

From the arangojs README : 来自arangojs 自述文件

// Or using a fully qualified URL containing the database path
const db = new Database({
  url: `http://${username}:${password}@${host}:${port}/_db/${database}`,
  databaseName: false // don't automatically append database path to URL
});
  • databaseName: string (Default: _system) databaseName:string(默认值:_system)

    Name of the active database. 活动数据库的名称。

    If this option is explicitly set to false, the url is expected to contain the database path and the useDatabase method can not be used to switch databases. 如果将此选项显式设置为false,则该URL应包含数据库路径,并且useDatabase方法不能用于切换数据库。

So you either specify the full path like http://host:port/_db/dbname and set databaseName: false , or you use http://host:port and either specify databaseName: "dbname" or useDatabase("dbname") . 因此,您可以指定http://host:port/_db/dbname和set databaseName: false类的完整路径,也可以使用http://host:port并指定databaseName: "dbname"useDatabase("dbname")

/_api should not be part of the path, ever. /_api不应该成为路径的一部分。 arangojs knows where to find the API endpoint and does it for you. arangojs知道在哪里可以找到API端点并为您完成。

var arangojs = require('arangojs');

// Const variables for connecting to ArangoDB database
const host = '127.0.0.1'
const port = '8529'
const username = 'xyz'
const password = 'xyz'
const databasename = 'sgcdm_app'

 // Connection to ArangoDB
db = new arangojs.Database({
    url: `http://${host}:${port}`,
    databaseName: databasename
});
db.useBasicAuth(username, password);

db.listCollections().then(function(res) {
    res.forEach((coll, i) => {
        console.log(`${i+1}. ${coll.name} (ID=${coll.id}, system=${coll.isSystem})`)
    });
}, function(err) {
    const res = err.response.body;
    console.log(`Error ${res.errorNum}: ${res.errorMessage} (HTTP ${res.code})`);
});

On success (database exists): 成功时(数据库存在):

1. first (ID=264349479, system=false)
2. test (ID=264349463, system=false)

On error (unknown database): 出错(未知数据库):

Error 1228: database not found (HTTP 404)

cross-post from GitHub 来自GitHub的邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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