【问题标题】:How can I access methods inside a class from outside of class如何从类外访问类内的方法
【发布时间】:2019-10-15 08:00:33
【问题描述】:

我有几个关于类文件的问题。我有以下课程

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 $1 in interests LIMIT 1"),
                [queryString],
                callback(err, result)
              )     
            });
          }
  }

我的问题是如何从类文件外部访问 doSomeQuery 函数?在内部访问该函数没有问题,但我需要能够从外部调用它。 我试过这样的事情

const CouchController = require("../controllers/CouchController")(couchbase, config)
let newTest = new CouchController

这样做 newTest 永远不会公开 doSomeQuery 方法。

还有什么方法的限制?它只能是一个简单的,还是可以是异步的并使用承诺等?

【问题讨论】:

  • newTest.doSomeQuery() ?

标签: javascript node.js couchbase


【解决方案1】:

我来来回回地想通了,我的部分问题是由于某种原因,Visual Studio 代码没有向我显示让我失望的方法。手动输入最终使其发挥作用。

这是我的班级,我实际上将配置和沙发库本身移到了班级文件中,因此不再需要传递它。

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

这就是我现在如何调用我的类并连接到后端来获取我的数据。

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

【讨论】:

    【解决方案2】:

    对于以下问题,您应该考虑两件主要的事情。

    1. 先正确导出。我不确定您是否打算将其排除在外,但将类导出为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 $1 in interests LIMIT 1"),
                    [queryString],
                    callback(err, result)
                  )     
                });
              }
      }
    
    1. 类初始化有点不正确。您可以在此 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);
    

    【讨论】:

      【解决方案3】:

      导入后需要实例化类

      更改以下内容

      const CouchController = require("../controllers/CouchController")(couchbase, config)
      let newTest = new CouchController
      

      const CouchController = require("../controllers/CouchController")
      let newTest = new CouchController(couchbase, config)
      

      你还需要像这样导出你的类

      export default class CouchController {

      然后像这样访问方法

      newTest.doSomeQuery(...)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-14
        • 2011-01-02
        • 1970-01-01
        • 2020-11-06
        相关资源
        最近更新 更多