如何从 class 外部访问 class 内部的方法
How can I access methods inside a class from outside of class
我有几个关于 Class 文件的问题。我有以下 Class
class CouchController {
constructor(couchbase, config) {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
doSomeQuery(queryString, callback) {
this.bucket.manager().createPrimaryIndex(function() {
this.bucket.query(
this.N1qlQuery.fromString("SELECT * FROM bucketname WHERE in interests LIMIT 1"),
[queryString],
callback(err, result)
)
});
}
}
我的问题是如何从 class 文件外部访问 doSomeQuery 函数?在内部访问函数没有问题,但我需要能够从外部调用它。
我试过这样的东西
const CouchController = require("../controllers/CouchController")(couchbase, config)
let newTest = new CouchController
这样做 newTest 永远不会公开 doSomeQuery 方法。
还有一个方法的局限性是什么?它只能是一个简单的还是可以是异步的并使用 promises 等?
导入后需要实例化class
更改以下内容
const CouchController = require("../controllers/CouchController")(couchbase, config)
let newTest = new CouchController
到
const CouchController = require("../controllers/CouchController")
let newTest = new CouchController(couchbase, config)
您还需要像这样导出 class
export default class CouchController {
然后是这样的访问方式
newTest.doSomeQuery(...)
对于以下问题,您应该考虑 2 个主要问题。
- 先正确导出。我不确定您是否打算忽略它,但是导出 class 以便在外部用作
require
很重要。如果您需要技术细节,请点击此处 NodeJS exports documentation。
// common module default export
module.exports = class CouchController {
constructor(couchbase, config) {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
doSomeQuery(queryString, callback) {
this.bucket.manager().createPrimaryIndex(function() {
this.bucket.query(
this.N1qlQuery.fromString("SELECT * FROM bucketname WHERE in interests LIMIT 1"),
[queryString],
callback(err, result)
)
});
}
}
- class 初始化稍有不正确。您可以在 here 上查看文档。您可以将要求和初始化更改为...
const CouchController = require('../controllers/CouchController');
const newTest = new CouchController(couchbase, config);
// now you can access the function :)
newTest.doSomeQuery("query it up", () => {
// here is your callback
})
如果你使用的是 ES6 模块或打字稿,你可以导出类似...
export default class CouchController {
// ...
}
... 并导入类似...
import CouchController from '../controllers/CouchController';
const newTest = new CouchController(couchbase, config);
我来回想了想,我的部分问题是由于某种原因 visual studio 代码没有向我展示让我失望的方法。手动输入使它最终有用。
这是我的 Class,我实际上将配置和 couchbase 本身移到了 class 文件中,因此无需再传递它。
const couchbase = require("couchbase")
const config = require("../config/config")
class CouchController {
constructor() {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = this.cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
getDoc2(docID){
return new Promise((resolve,reject)=>{
this.bucket.get(docID ,(err, result)=>{
if(err) return reject(err);
return resolve({docID,result});
});
});
}
}
module.exports = CouchController
这就是我现在如何调用我的 Class 并连接到后端以获取我的数据。
const CouchController = require("./controllers/CouchController")
let newTest = new CouchController
const test= async()=>{
let { docID, result } = await newTest.getDoc2("grid_info::20b05192-79e9-4e9d-94c9-91a4fc0a2765")
console.log(docID)
console.log(result)
}
我有几个关于 Class 文件的问题。我有以下 Class
class CouchController {
constructor(couchbase, config) {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
doSomeQuery(queryString, callback) {
this.bucket.manager().createPrimaryIndex(function() {
this.bucket.query(
this.N1qlQuery.fromString("SELECT * FROM bucketname WHERE in interests LIMIT 1"),
[queryString],
callback(err, result)
)
});
}
}
我的问题是如何从 class 文件外部访问 doSomeQuery 函数?在内部访问函数没有问题,但我需要能够从外部调用它。 我试过这样的东西
const CouchController = require("../controllers/CouchController")(couchbase, config)
let newTest = new CouchController
这样做 newTest 永远不会公开 doSomeQuery 方法。
还有一个方法的局限性是什么?它只能是一个简单的还是可以是异步的并使用 promises 等?
导入后需要实例化class
更改以下内容
const CouchController = require("../controllers/CouchController")(couchbase, config)
let newTest = new CouchController
到
const CouchController = require("../controllers/CouchController")
let newTest = new CouchController(couchbase, config)
您还需要像这样导出 class
export default class CouchController {
然后是这样的访问方式
newTest.doSomeQuery(...)
对于以下问题,您应该考虑 2 个主要问题。
- 先正确导出。我不确定您是否打算忽略它,但是导出 class 以便在外部用作
require
很重要。如果您需要技术细节,请点击此处 NodeJS exports documentation。
// common module default export
module.exports = class CouchController {
constructor(couchbase, config) {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
doSomeQuery(queryString, callback) {
this.bucket.manager().createPrimaryIndex(function() {
this.bucket.query(
this.N1qlQuery.fromString("SELECT * FROM bucketname WHERE in interests LIMIT 1"),
[queryString],
callback(err, result)
)
});
}
}
- class 初始化稍有不正确。您可以在 here 上查看文档。您可以将要求和初始化更改为...
const CouchController = require('../controllers/CouchController');
const newTest = new CouchController(couchbase, config);
// now you can access the function :)
newTest.doSomeQuery("query it up", () => {
// here is your callback
})
如果你使用的是 ES6 模块或打字稿,你可以导出类似...
export default class CouchController {
// ...
}
... 并导入类似...
import CouchController from '../controllers/CouchController';
const newTest = new CouchController(couchbase, config);
我来回想了想,我的部分问题是由于某种原因 visual studio 代码没有向我展示让我失望的方法。手动输入使它最终有用。
这是我的 Class,我实际上将配置和 couchbase 本身移到了 class 文件中,因此无需再传递它。
const couchbase = require("couchbase")
const config = require("../config/config")
class CouchController {
constructor() {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = this.cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
getDoc2(docID){
return new Promise((resolve,reject)=>{
this.bucket.get(docID ,(err, result)=>{
if(err) return reject(err);
return resolve({docID,result});
});
});
}
}
module.exports = CouchController
这就是我现在如何调用我的 Class 并连接到后端以获取我的数据。
const CouchController = require("./controllers/CouchController")
let newTest = new CouchController
const test= async()=>{
let { docID, result } = await newTest.getDoc2("grid_info::20b05192-79e9-4e9d-94c9-91a4fc0a2765")
console.log(docID)
console.log(result)
}