To create a remote database using new PouchDB() does not work as expected

354 views
Skip to first unread message

Ying Bian

unread,
Oct 4, 2015, 6:54:33 AM10/4/15
to PouchDB
I'm using pouchdb 4.0.3 with couchdb 2.0.0 (from master branch). I try to create a remote database using the following code:

var PouchDB = require('pouchdb')
var db = new PouchDB('http://localhost:5984/testdb') # testdb doesn't exist yet
  .then(function() 
    { console.log('OK'); }
  )

I got the output "OK"  from the console but actually that specific database still does not exist:

{"error":"not_found","reason":"Database does not exist."}

The db will only be created if I do some operations with it like 'db.info()' or 'db.put()'.  

Is this the design? Why?


Dale Harvey

unread,
Oct 4, 2015, 7:59:45 AM10/4/15
to pou...@googlegroups.com
Yup this is by design, it was changed in https://github.com/pouchdb/pouchdb/commit/f8c06ac76d321e84bdcab25f9cf02f5d648e06af and can see some of the discussion about the change @ https://github.com/pouchdb/pouchdb/issues/4049

Basically the synchronous constructor complicated error handling for users, having the constructor return a promise (how you are using it in that example) is pretty broken (its a promise that promises itself) and having to wrap the constructor in try { } is messy, especially when it comes to something like db.sync('http://example.org/mydb');

So now we dont actually do anything in constructor (for http for now, but will likely move the rest to the same model) and defer setup until the first operation is called.

If you want to ensure the database is created, as you hinted to the best way is to new PouchDB(name).info()

Cheers
Dale

--
You received this message because you are subscribed to the Google Groups "PouchDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pouchdb+u...@googlegroups.com.
To post to this group, send email to pou...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pouchdb/88a07a41-4b81-4150-9f46-fcc79674d09b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ying Bian

unread,
Oct 4, 2015, 6:49:15 PM10/4/15
to PouchDB
Cool! This is just the confirmation I was looking for. Thanks, Dale!

Josh Wainer

unread,
May 29, 2017, 5:07:44 PM5/29/17
to PouchDB
Hi Dale,

Thanks for the reply but when I do .info() I get 404 not found. the db is actually not created. curl http://localhost:5984/notes gets {"error":"not_found","reason":"Database does not exist."}
The odd part is if I console out the object the constructor returns, it looks ok.
BTW, no code change on my part, simply deleted couchdb 1.6.x and installed 2.0

Thanks,

Josh
Reply all
Reply to author
Forward
0 new messages