Quispiam

power computing

I was tooling around with cradle(https://github.com/cloudhead/cradle) and wanted to create a database if it didn’t exist. Sounds easy enough right, easy peasy ? I thought so too but could not get the code in the documentation to work.

db.exists(function (err, exists) {
    if (err) {
      console.log('error', err);
    } else if (exists) {
      console.log('the force is with you.');
    } else {
      console.log('database does not exists.');
      db.create();
      /* populate design documents */
    }
  });

Because I am a noob at node and functional programming in general I neglected to see that the create function should take a callback function as an argument. Reading through the source code led me to create the following mess of code to create a database if it doesnt exist.

db.exists(function (err, exists) {
    if (err) {
      console.log('error', err);
    } else if (exists) {
      console.log('the force is with you.');
    } else {
      console.log('database does not exists.');
      db.create(function(error,response){
         if(error) {
            console.log('error', error)
         } else {
            console.log('response', response);
         }
      });
      /* populate design documents */
    }
  });

So all in all just a couple of wasted hours :)

Thanks go to:

The cradle source

tagio.net, for whom the code was written.

Matt

Share