Node.js Access mongodb连接变量在环回中全局可访问
如何在环回中全局访问mongo db连接变量。目前,它是通过执行异步:Node.js Access mongodb连接变量在环回中全局可访问,node.js,mongodb,loopbackjs,Node.js,Mongodb,Loopbackjs,如何在环回中全局访问mongo db连接变量。目前,它是通过执行异步: Alerts.getDataSource().connector.connect(function (err, db) { db.collection("dbname"); ......................//other code }); 是否可以从模型中的任何位置直接访问此db变量,而不是调用connector.connect everywhere?提前谢谢 您只需要连接池。给你 //
Alerts.getDataSource().connector.connect(function (err, db) {
db.collection("dbname");
......................//other code
});
是否可以从模型中的任何位置直接访问此db变量,而不是调用connector.connect everywhere?提前谢谢 您只需要连接池。给你
// This is a global variable to use for handing the MongoDB client
var mongodb;
// Connection URL
var url = '[connectionString]';
// Create the db connection
Alerts.getDataSource().connector.connect(function (err, db) {
mongodb = db
......................//other code
});
这里有一个更好的解释