简体   繁体   English

使用 node.js 的 ArangoDB 中的数据库类型错误

[英]Database TypeError in ArangoDB with node.js

Applying this tutorial without Foxx:在没有 Foxx 的情况下应用本教程:

http://www.ashishblog.com/getting-start-with-arangodb-using-nodejs-nodejs-ejs-arangojs/ http://www.ashishblog.com/getting-start-with-arangodb-using-nodejs-nodejs-ejs-arangojs/

Node.js 8.11.1 (x64)节点 8.11.1 (x64)

arangoDB 3.3.7-1_win64 arangoDB 3.3.7-1_win64

arangojs@6.2.4 arangojs@6.2.4

ERROR MESSAGES IN THE BROWSER浏览器中的错误信息

@ http://localhost:3000/users @ http://localhost:3000/用户

 TypeError: db.database is not a function at Object.getAllUsers (H:\TEST\app\services\DataServices.js:6:13) at H:\TEST\app\routes\users.js:8:11 at Layer.handle [as handle_request] (H:\TEST\app\node_modules\express\lib\router\layer.js:95:5) at next (H:\TEST\app\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (H:\TEST\app\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (H:\TEST\app\node_modules\express\lib\router\layer.js:95:5) at H:\TEST\app\node_modules\express\lib\router\index.js:281:22 at Function.process_params (H:\TEST\app\node_modules\express\lib\router\index.js:335:12) at next (H:\TEST\app\node_modules\express\lib\router\index.js:275:10) at Function.handle (H:\TEST\app\node_modules\express\lib\router\index.js:174:3)

Services\ DataServices.js:服务\ DataServices.js:

 var Database = require('arangojs'); var db = new Database({url:'http://127.0.0.1:8529'}); module.exports = { getAllUsers: function() { return db.database('nodeArangoWebAppDB').then(function (mydb) {return mydb.query('FOR x IN User RETURN x');}).then(function (cursor) { return cursor.all();}); }, getUserByKey: function(userKey) { var bindVars = {'userKey': userKey}; return db.database('nodeArangoWebAppDB').then(function (mydb) {return mydb.query('FOR x IN User FILTER x._key == @userKey RETURN x',bindVars);}).then(function (cursor) { return cursor.all();}); }, addUser: function(user) { return db.database('nodeArangoWebAppDB').then(function (mydb) {return mydb.collection('User');}).then(function (collection) { return collection.save(user);}); }, updateUser: function(user) { var bindVars = {'key': user.key, 'username': user.username,"email":user.email }; return db.database('nodeArangoWebAppDB').then(function (mydb) {return mydb.query('FOR x IN User FILTER x._key == @key UPDATE x WITH { username:@username, email:@email } IN User',bindVars );}).then(function (cursor) { return cursor.all();}); }, removeUser: function(userKey) { var bindVars = {'userKey': userKey}; return db.database('nodeArangoWebAppDB').then(function (mydb) {return mydb.query('FOR x IN User FILTER x._key == @userKey REMOVE x IN User LET removed = OLD RETURN removed', bindVars);}).then(function (cursor) {return cursor.all();}); } }

What is wrong with this:这有什么问题:

 return db.database('nodeArangoWebAppDB')

What would be the right coding (without Foxx)?什么是正确的编码(没有 Foxx)?

What changes should be made implementing Foxx?实施 Foxx 应该做出哪些改变?


EDIT #1:编辑#1:

What is wrong with this:这有什么问题:

 var db = new arangojs.Database('http://127.0.0.1:8529') db.useDatabase("nodeArangoWebAppDB"); db.useBasicAuth("root", "root"); module.exports = { getAllUsers: function(){ return db._query('FOR x IN User RETURN x').then(value) => { return value.all();}; },

 H:\TEST\app>npm start > app@0.0.0 start H:\TEST\app > node./bin/www H:\TEST\app\services\DataServices.js:8 return db._query('FOR x IN User RETURN x') ^ SyntaxError: Unexpected token. at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (H:\TEST\app\routes\users.js:3:15) npm ERR. code ELIFECYCLE npm ERR. errno 1 npm ERR: app@0.0.0 start. `node./bin/www` npm ERR. Exit status 1 npm ERR. npm ERR! Failed at the app@0.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Should we use Webpack?我们应该使用 Webpack 吗?

I have been working on this. 我一直在努力。 The below code seems to work 下面的代码似乎有效

var arangojs = require("arangojs");
var db = new arangojs.Database('http://127.0.0.1:8529');
db.useDatabase("superrango");
db.useBasicAuth("root", "asdf");

module.exports = {
    getAllUsers : function()
    {
                 return db.query('FOR x IN Users RETURN x')
                         .then((value) => {return value.all();});

    }
}

I know this is a couple years later, but I had the exact same issue after changing to version 8 of the arangojs module in node.我知道这是几年后的事了,但是在更改为 node.js 中的 arangojs 模块的第 8 版后,我遇到了完全相同的问题。 Turns out, to use a different database you supply this in an object in the database constructor:事实证明,要使用不同的数据库,您可以在数据库构造函数的对象中提供它:

const db = new arangojs.Database({
  url:'http://172.25.255.4:8529',
  databaseName: 'my_database',
  auth: { username:'root', password:'my_assword'}
})

The tutorial I was following specified using v5 and used the 'old' methods to connect.我遵循的教程指定使用 v5 并使用“旧”方法进行连接。 So with (at least v8), you supply the connection as an object.因此,使用(至少 v8),您将连接作为对象提供。

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

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