I am trying to connect my couchbase server but i am getting warning msg when i an trying to create a connection instance on app.js on my express.js app.
Here is the 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));
Here is my 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;
when i run node app.js getting this error
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)
//RESULT AFTER CHANGES
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 model
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';
}
}
GETTING THIS ERROR
TypeError: Cannot read properties of undefined (reading 'get')
CodePudding user response:
The circular dependency warning is likely because you are exporting the bucket from app.js
, requiring it in your model, which is also likely included somewhere in app.js
, forming a circular dependency.
To fix this, I would suggest moving the connection code into a separate file, so that you can require it wherever needed in your models and routes.
Important note: If you are using the Node.js SDK version 3.0 or above, you'll need to call .get()
on a collection level object, rather than on the bucket. You can easily just export the collection you need from your connection code. See the following example of a connection file, which uses default scope and collection:
// 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;
});
You can then easily require connection.js
in your model and obtain bucket, cluster, or collection level objects as needed:
const connection = require('../connection');
const data_bucket = connection.bucket;
const data_cluster = connection.cluster;
const data_collection = connection.defaultCollection;