gpt4 book ai didi

Node.js 重用 MongoDB 引用

转载 作者:IT老高 更新时间:2023-10-28 13:08:33 24 4
gpt4 key购买 nike

我无法理解 node.js。

例如,MongoDB 访问,这是我所拥有的 (mydb.js):

var mongodb = require('mongodb'),
server = new mongodb.Server('staff.mongohq.com', 10030, {
auto_reconnect: true
}),
db = new mongodb.Db('mydb', server);

function authenticateAndGo(db, handle) {
db.authenticate('username', 'password', function(err) {
if (err) {
console.log(err);
return;
}
console.log('Database user authenticated');

var collection = new mongodb.Collection(db, 'test');

handle(collection);
});
}

function query(handle) {
db.open(function(err, db) {
if( err ) {
console.log(err);
return;
}
console.log('Database connected');

authenticateAndGo(db, handle);
});
};
exports.query = query;

所以,如果我以后想使用它,我会

var mydb = require('./mydb');
mydb.query(function(collection) {
collection.find({}, {
limit: 10
}).toArray(function(err, docs) {
console.log(docs);
});
});

但是,如果我打多个电话,像这样:

var mydb = require('./mydb');
mydb.query(function(collection) {
collection.find({}, {
limit: 10
}).toArray(function(err, docs) {
console.log(docs);
});
});
mydb.query(function(collection) {
collection.find({}, {
limit: 10
}).toArray(function(err, docs) {
console.log(docs);
});
});

我得到一个异常(exception):

Error: db object already connecting, open cannot be called multiple times

我认为关于这一切,我确实有一些基本的东西我不明白,而且这个问题很可能是愚蠢的......

无论如何,欢迎所有帮助。

提前致谢。

最佳答案

mydb.js:

var mongodb= require('mongodb'),
server = new mongodb.Server('staff.mongohq.com', 10030, {
auto_reconnect: true
}),
db1 = new mongodb.Db('mydb', server);


// callback: (err, db)
function openDatabase(callback) {
db1.open(function(err, db) {
if (err)
return callback(err);

console.log('Database connected');

return callback(null, db);
});
}

// callback: (err, collection)
function authenticate(db, username, password, callback) {
db.authenticate(username, password, function(err, result) {
if (err) {
return callback (err);
}
if (result) {
var collection = new mongodb.Collection(db, 'test');

// always, ALWAYS return the error object as the first argument of a callback
return callback(null, collection);
} else {
return callback (new Error('authentication failed'));
}
});
}

exports.openDatabase = openDatabase;
exports.authenticate = authenticate;

use.js:

var mydb = require('./mydb');
// open the database once
mydb.openDatabase(function(err, db) {
if (err) {
console.log('ERROR CONNECTING TO DATABASE');
console.log(err);
process.exit(1);
}

// authenticate once after you opened the database. What's the point of
// authenticating on-demand (for each query)?
mydb.authenticate(db, 'usernsame', 'password', function(err, collection) {
if (err) {
console.log('ERROR AUTHENTICATING');
console.log(err);
process.exit(1);
}

// use the returned collection as many times as you like INSIDE THE CALLBACK
collection.find({}, {limit: 10})
.toArray(function(err, docs) {
console.log('\n------ 1 ------');
console.log(docs);
});

collection.find({}, {limit: 10})
.toArray(function(err, docs) {
console.log('\n------ 2 ------');
console.log(docs);
});
});
});

结果:

成功:

 Database connected
Database user authenticated

------ 1 ------
[ { _id: 4f86889079a120bf04e48550, asd: 'asd' } ]

------ 2 ------
[ { _id: 4f86889079a120bf04e48550, asd: 'asd' } ]

失败时:

Database connected
{ [MongoError: auth fails] name: 'MongoError', errmsg: 'auth fails', ok: 0 }

[原答案]:

您将多次打开 db(在每个 query 中打开一次)。您应该只打开一次数据库,并在回调中使用 db 对象以供以后使用。

您多次使用同一个变量名,这可能会造成一些困惑。

var mongodb = require('mongodb'),
server = new mongodb.Server('staff.mongohq.com', 10030, {
auto_reconnect: true
}),
db1 = new mongodb.Db('mydb', server);

function authenticateAndGo(db, handle) {
db.authenticate('username', 'password', function(err) {
if (err) {
console.log(err);
return;
}
console.log('Database user authenticated');

var collection = new mongodb.Collection(db, 'test');

handle(collection);
});
}

function query(handle) {
db1.open(function(err, db2) {
if( err ) {
console.log(err);
return;
}
console.log('Database connected');

authenticateAndGo(db2, handle);
});
};
exports.query = query;

我对上面的代码做了一些改动(db1 用于原始数据库,db2 用于打开 数据库)。如您所见,您多次打开 db1,这并不好。提取代码以打开另一个方法并使用它一次并使用 db2 实例进行所有查询/更新/删除/...

关于Node.js 重用 MongoDB 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10108170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com