max-mapper / dat-core

low level implementation of the dat data version graph
MIT License
42 stars 6 forks source link

Executing "Usage Example" => Error: No dat here #26

Open serapath opened 9 years ago

serapath commented 9 years ago

Do I need to pass in a leveldb? The readme says something about

(default is require('leveldown'))

I do

var dat = require('dat-core')

var db = dat('./test')

db.put('hello', 'world', function (err) { // insert value 
  if (err) return handle(err) // something went wrong 
  db.get('hello', function (err, result) {
    if (err) return handle(err) // something went wrong 
    console.log(result)   // prints result 
    console.log(db.head) // the 'head' of the database graph (a hash) 
  })
})

// function handle (err) { console.error(err); }

and get

events.js:141
      throw er; // Unhandled 'error' event
            ^
Error: No dat here
    at /home/serapath/EXPERIMENTS/DAT_CORE/node_modules/dat-core/index.js:213:44
    at FSReqWrap.cb [as oncomplete] (fs.js:212:19)
mafintosh commented 9 years ago

did you dat init in the ./test folder?

serapath commented 9 years ago

nope, i wasn't aware that i need dat to use dat-core. I just tried an npm install dat -g, but ran into problems during installation. npm install dat fails too. Maybe I can post that on the dat repo instead?

max-mapper commented 9 years ago

@serapath you can call db.init from dat-core: https://github.com/maxogden/dat-core#dbinitcb, i think thats what @mafintosh meant

serapath commented 9 years ago

Hm, probably I'm doing something stupid.

// [./index.js]
var dat = require('dat-core');
var db = dat('./test');
db.init(function(){console.log(arguments);});

$> node index.js

events.js:141 throw er; // Unhandled 'error' event ^ Error: No dat here ...

But var db = dat('./test') throws anyway. I now solved it by writing:

var dat = require('dat-core');
var db = dat('./test', {createIfMissing:true});

I'm not sure if that's how it's meant to be, but if so, then an update of the usage example might be helpful.

mafintosh commented 9 years ago

Ah yea. init should probably do that for you. I'll do a fix for that

okdistribute commented 9 years ago

how about this? https://github.com/maxogden/dat-core/pull/30