首页 文章

带有node.js的ArangoDB中的数据库类型错误

提问于
浏览
0

在没有Foxx的情况下应用本教程

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

Node.js 8.11.1(x64)

arangoDB 3.3.7-1_win64

arangojs@6.2.4

浏览器中的错误信息

@ http://localhost:3000/users

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:

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();});
	}	
}

这有什么问题:

return db.database('nodeArangoWebAppDB')

什么是正确的编码(没有Foxx)?

实施Foxx应该做些什么改变?


编辑#1:

这有什么问题:

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.

我们应该使用Webpack吗?

1 回答

  • 0

    我一直在努力 . 以下代码似乎有效

    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();});
    
        }
    }
    

相关问题