私が設計しているモジュールは、mongodbインスタンスをパラメーターとして受け取るコンストラクター関数を提供します。私のアプリケーションでは、マングースを使用してこれをテストしようとしています。mongooseはmongoDBドライバーモジュール上に構築されているため、mongooseモジュールからdbドライバーオブジェクトを取得する方法があると思います。
失敗している機能がありますが、その理由がわかりません。
更新
以下は私のモジュールからのコードです
//authorizer.js
function Authorizer(mongoDBCon, userOptions){
this.db = mongoDBCon;
this.authorize = authorize;
this.assignOwner = assignOwner;
this.setUserPermission = setUserPermission;
this.setRolePermission = setRolePermission;
this.retrieveUserPermission = retrieveUserPermission;
this.setRolePermission = setRolePermission;
let defaultOptions = {
unauthorizedHandler: (next)=>{
let error = new Error('user has performed an unauthorized action');
error.status = 401;
next(error);
},
userAuthCollection: 'user_authorization',
roleAuthCollection: 'role_authorization',
}
this.options = _.assign({},defaultOptions,userOptions);
}
function setRolePermission(role, resource, permissions) {
let document = {
role: role,
resource: resource,
permissions: permissions,
};
//add the document only if the role permission is not found
let collection = this.db.collection(this.options.roleAuthCollection);
collection.findOne(document)
.then((result)=>console.log('in here')) //------> not printing :(
.catch(e=>{console.log(e)});
}
構成するには、別のファイルにインポート/要求する必要があります
//authorizerConfig
let mongoose = require('mongoose');
let Authorizer = require('util/authorization/authorization');
let authorizer = new Authorizer(mongoose.connection.db);
//set admin role permissions
authorizer.setRolePermission('admin', 'users', '*');
authorizer.setRolePermission('admin', 'postings', '*');
module.exports = authorizer;
mongoに接続されたファイル
//app.js
// Set up and connect to MongoDB:
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect(process.env.MONGODB_URI);//MONGODB_URI=localhost:27017/house_db
then()
メソッドで表示したいと思っていたログが表示されなくなりました。
回答: @NeilLunnから回答がありました。要約すると、mongoose.connection.dbは、MongoClient.connectから返されるdbと同等です。また、接続を確立する前にデータベースにクエリを実行していたため、エラーが発生しました。
MongoClientと基盤となるノードドライバーは確かにpromiseをサポートしています。実際に使用している方法が何であれ、正しい「データベースオブジェクト」を参照していないということです。
デモンストレーションとして:
const mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.set('debug',true);
const uri = 'mongodb://localhost/other', // connect to one database namespace
options = { useMongoClient: true };
function log(data) {
console.log(JSON.stringify(data,undefined,2))
}
(async function() {
try {
const conn = await mongoose.connect(uri,options);
const testDb = conn.db.db('test'); // For example, get "test" as a sibling
let result = await testDb.collection('test').find().toArray();
log(result);
} catch(e) {
console.error(e);
} finally {
mongoose.disconnect();
}
})();
したがって、あなたが「すべき」ことは、接続から「接続」オブジェクトを取得し、db
そこから参照することです。db
現在の接続の「兄弟」スペースが必要になる可能性があり"admin"
ます。特定のケースでは、「認証」の詳細を取得する必要があります。
しかし、ここでは、「名前付き兄弟」にアクセスするために.db()
、Db
オブジェクトのメソッドを使用しています。これは顕著ではないと同じように、非同期メソッド.collection()
非同期ではありません。
そこからは、コアドライバーからそれぞれのオブジェクトにネイティブな他のメソッドを実装するだけです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加