简体   繁体   English

数据库引用未在节点 js 上的 arangojs 中定义

[英]Database reference is not defined in arangojs on node js

I am trying to use ArangoJS drive on Node JS, when I add following code to main app.js file it works but when added to a function in a class in different file it throws an error.我正在尝试在 Node JS 上使用 ArangoJS 驱动器,当我将以下代码添加到主 app.js 文件时它可以工作,但是当添加到不同文件中 class 中的 function 时,它会引发错误。

Database is not defined数据库未定义

Code is as follows代码如下

class User {
insertUser() {
        Database = require('arangojs').Database;
        db = new Database();
        db.useBasicAuth("", "");
        db.useDatabase('_system');

        collection = db.collection('Users');
        doc = {
          _key: 'firstDocument',
          a: 'foo',
          b: 'bar',
          c: Date()
        };
        collection.save(doc).then(
          meta => console.log('Document saved:', meta._rev),
          err => console.error('Failed to save document:', err)
        );
}
contructor() {

}
}



 module.exports = User;

If I use the code from insertUser function in app.js directly it works fine.如果我直接在 app.js 中使用 insertUser function 中的代码,它可以正常工作。

Kindly advice好心劝告

Thanks谢谢

Note: I am using Express with Node js注意:我使用 Express 和 Node js

You need to require database outside of the class and use const:您需要 class 之外的数据库并使用 const:

const { Database } = require('arangojs');
class User {
contructor() {}

insertUser() {
        db = new Database();
        db.useBasicAuth("", "");
        db.useDatabase('_system');

        collection = db.collection('Users');
        doc = {
          _key: 'firstDocument',
          a: 'foo',
          b: 'bar',
          c: Date()
        };
        collection.save(doc).then(
          meta => console.log('Document saved:', meta._rev),
          err => console.error('Failed to save document:', err)
        );
}

}



 module.exports = User;

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

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