问题
Applying this tutorial without 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
ERROR MESSAGES IN THE BROWSER
@ 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();});
}
}
What is wrong with this :
return db.database('nodeArangoWebAppDB')
What would be the right coding (without Foxx) ?
What changes should be made implementing Foxx ?
EDIT #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 ?
回答1:
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();});
}
}
来源:https://stackoverflow.com/questions/50280066/database-typeerror-in-arangodb-with-node-js