【问题标题】:Couchase Node Js sdk connectionCouchbase 节点 Js sdk 连接
【发布时间】:2022-01-05 01:55:47
【问题描述】:

我正在尝试连接我的 couchbase 服务器,但是当我尝试在我的 express.js 应用上的 app.js 上创建连接实例时收到警告消息。

这里是 app.js

const express = require('express');
const bodyParser = require('body-parser');
const couchbase = require('couchbase');

const app = express();
const userRoutes = require('./routes/route');


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

//COUCHBASE CONNECTION
const cluster = couchbase.connect('couchbase://localhost', {username: 'admin', password: '5121451214'},(err,cluster) => { 
    const bucket =  cluster.bucket('test_data_bucket');
    module.exports.bucket = bucket;
}); 



app.use('/',userRoutes);

app.listen(process.env.port || 3000);

console.log('Web Server is listening at port '+ (process.env.port || 3000));

这是我的AutherModel

const N1qlQuery = require('couchbase').N1qlQuery;
const data_bucket = require('../app').bucket;


function AuthorModel (){}

AuthorModel.getAll = function (callback)
{
    data_bucket.get('invoice_32212100000218', (err, res) => {
        if(error)
        {
            return callback(error,null);
        }

        callback(null,res);
    });
}


module.exports.AuthorModel = AuthorModel;

当我运行 node app.js 时出现此错误

node:2410417) Warning: Accessing non-existent property 'bucket' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

//更改后的结果

connection.js

const couchbase = require('couchbase');

//COUCHBASE CONNECTION
couchbase.connect('couchbase://localhost', {username: 'admin', password: '5121451214'},(err,cluster) => {
  const bucket =  cluster.bucket('test_data_bucket');
  const defautScope = bucket.scope('_default');
  const defaultCollection = defautScope.collection('_default');

  module.exports.bucket = bucket;
  module.exports.cluster = cluster;
  module.exports.defaultCollection = defaultCollection;
});

author.js 模型

 const connection = require('../config/connection');
    
    const data_bucket = connection.bucket;
    const data_cluster = connection.cluster;
    const data_collection = connection.defaultCollection;
    
    
    module.exports = {
        getUsers  ()
        {
            const data = data_collection.get('invoice_32212100000218', (err, res) => {
                console.log(res);
            });
    
            return 'Hello from express';
        }
     }

收到此错误

TypeError: Cannot read properties of undefined (reading 'get')

【问题讨论】:

    标签: node.js express couchbase


    【解决方案1】:

    循环依赖警告很可能是因为您正在从app.js 导出存储桶,需要在您的模型中使用它,它也可能包含在app.js 的某处,从而形成循环依赖。

    要解决此问题,我建议将连接代码移动到单独的文件中,以便您可以在模型和路由中需要它的任何地方使用它。

    重要提示:如果您使用的是 Node.js SDK 3.0 或更高版本,则需要在集合级别对象上调用.get(),而不是在存储桶上。您可以轻松地从连接代码中导出所需的集合。请参阅以下使用默认范围和集合的连接文件示例:

    // connection.js
    const couchbase = require('couchbase')
    
    //COUCHBASE CONNECTION
    const cluster = couchbase.connect('couchbase://localhost', {username: 'admin', password: '5121451214'},(err,cluster) => {
      const bucket =  cluster.bucket('test_data_bucket');
      const defautScope = bucket.scope('_default');
      const defaultCollection = defautScope.collection('_default')
    
      module.exports.bucket = bucket;
      module.exports.cluster = cluster;
      module.exports.defaultCollection = defaultCollection;
    });
    

    然后,您可以轻松地在模型中要求 connection.js 并根据需要获取存储桶、集群或集合级别的对象:

    const connection = require('../connection');
    
    const data_bucket = connection.bucket;
    const data_cluster = connection.cluster;
    const data_collection = connection.defaultCollection;
    

    【讨论】:

    • 感谢我通过您推荐的更改更新了帖子,但这次遇到了新问题,请您检查一下:)
    • 在导出默认集合之前在连接文件中调用时,相同的 get 方法工作正常
    • 尝试将 const data_collection = connection.defaultCollection; 移动到您的 getUsers() 函数中
    • 成功了,谢谢:)
    • 有没有办法在每个方法中避免这种情况 const data_collection = connection.defaultCollection
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多