本文介绍了带有node.js的ArangoDB中的数据库TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在没有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
浏览器中的错误消息
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:
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
> [email protected] 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! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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中的数据库TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!