我正在嘗試連接我的 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));
這是我的作者模型
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)
//修改后的結果
連接.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')
uj5u.com熱心網友回復:
回圈依賴警告可能是因為您正在從 匯出存盤桶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在模型中require并根據需要獲取存盤桶、集群或集合級別的物件:
const connection = require('../connection');
const data_bucket = connection.bucket;
const data_cluster = connection.cluster;
const data_collection = connection.defaultCollection;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/370571.html
