Couchase Node Js sdk 连接
Couchase Node Js sdk connection
我正在尝试连接我的 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;
当我 运行 节点 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')
循环依赖警告可能是因为您正在从 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;
我正在尝试连接我的 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;
当我 运行 节点 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')
循环依赖警告可能是因为您正在从 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;