使用 node.js 的 ArangoDB 中的数据库类型错误 [英] Database TypeError in ArangoDB with node.js

查看:20
本文介绍了使用 node.js 的 ArangoDB 中的数据库类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Applying this tutorial without Foxx :

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

Node.js 8.11.1 (x64)

Node.js 8.11.1 (x64)

arangoDB 3.3.7-1_win64

arangoDB 3.3.7-1_win64

arangojs@6.2.4

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)

服务\DataServices.js :

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)?

What would be the right coding (without Foxx) ?

实施 Foxx 应该做出哪些改变?

What changes should be made implementing 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 吗?

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

    }
}

这篇关于使用 node.js 的 ArangoDB 中的数据库类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆