I have the following code:
// Retrieve
var MongoClient = require("mongodb").MongoClient;
var accounts = null;
var characters = null;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/bq", function(err, db) {
if(err) { return console.dir(err); }
db.createCollection('accounts', function(err, collection) {
if(err) { return console.dir(err); }
else { accounts = collection; }
createAccount("bob","bob");
createAccount("bob","bob");
createAccount("bob","bob");
createAccount("bob","bob");
});
});
function createAccount(email, password)
{
accounts.findOne({"email":email}, function(err, item) {
if(err) { console.dir(err); }
else {
if(item === null) {
accounts.insert({"email":email, "password":password}, function(err, result) {
if(err) { console.dir(err); }
else { console.dir("Account " + email + " created."); }
});
}
else {
console.dir("Account already exists.")
}
}
});
}
When I run the script the first time, I end up with 4 accounts for bob. When I run it the second time, I get 4 messages that the account already exists.
I'm pretty sure I know why this is, and the solution I have come up with is to use some kind queue for processing each read/write of the database in order one at a time. What I am wanting to know, is whether that is the proper way to go about it, and what would the general best practice for this be?
Some languages provide a special language construct to deal with this problem. For example, C# has async
/await
keywords that let you write the code as if you were calling synchronous APIs.
JavaScript does not and you have to chain the createAccount
calls with callbacks.
Some people have developed libraries that may help you organize this code. For example async, step, node-promise and Q
You can also use the fibers library, a native library that extends the JavaScript runtime with fibers / coroutines.
And some people have extended the language with constructs that are similar to async
/await
: streamline.js, IcedCoffeeScript or wind.js. For example, streamline.js (I'm the author so I'm obviously biased) uses _
as a special callback placeholder and lets you write your example as:
var db = MongoClient.connect("mongodb://localhost:27017/bq", _):
var accounts = db.createCollection('accounts', _);
createAccount("bob","bob", _);
createAccount("bob","bob", _);
createAccount("bob","bob", _);
createAccount("bob","bob", _);
function createAccount(email, password, _) {
var item = accounts.findOne({"email":email}, _);
if (item === null) {
accounts.insert({"email":email, "password":password}, _);
console.log("Account " + email + " created."); }
} else {
console.log("Account already exists.")
}
}
And, last but not least, new language features such as generators and deferred functions are being discussed for future versions of JavaScript (generators are very likely to land in ES6, deferred functions seem to be a bit stalled).
So you have many options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With