我目前正在测试一些代码如何抵制以下场景:
要做到这一点,我有以下代码,使用官方驱动程序(在这里找到:https://github.com/mongodb/node-mongodb-native):
MongoClient.connect('mongodb://localhost:27017/testdb', function(err, db) {
app.get('/test', function(req, res) {
db.collection('users', function (err, collection) {
console.log(err);
if (err) {
// ## POINT 1 ##
// Handle the error
}
else {
collection.find({ 'username': username }, { timeout: true }).toArray(function(err, items) {
console.log(err);
if (err) {
// ## POINT 2 ##
// Handle the error
}
else {
if (items.length > 0) {
// Do some stuff with the document that was found
}
else …Run Code Online (Sandbox Code Playgroud)