From 248df73c85d91ae84b669775986b7dcb1778ff43 Mon Sep 17 00:00:00 2001 From: Benjamin Babik Date: Mon, 6 Apr 2020 01:38:23 +0100 Subject: [PATCH] correctly handle promise response from PouchDB.new See issue #374 --- .../express-pouchdb/lib/routes/db.js | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/packages/node_modules/express-pouchdb/lib/routes/db.js b/packages/node_modules/express-pouchdb/lib/routes/db.js index 60a9eb6b..987071e7 100644 --- a/packages/node_modules/express-pouchdb/lib/routes/db.js +++ b/packages/node_modules/express-pouchdb/lib/routes/db.js @@ -27,38 +27,39 @@ module.exports = function (app) { // PouchDB.new() instead of new PouchDB() because that adds // authorisation logic - var db = req.PouchDB.new(name, utils.makeOpts(req)); - db.info(function (err) { - if (err) { - // temoperary workaround for leveldb adapter, see - // https://github.com/pouchdb/pouchdb/issues/5668 - // when removing this code, also remove mkdir - // from dependencies in package.json - if (err.name === 'OpenError') { - var path = db.__opts.prefix ? db.__opts.prefix + name : name; - - mkdirp(pathResolve(path), function (err) { - if (err) { - return utils.sendError(res, err, 412); - } - - db = req.PouchDB.new(name, utils.makeOpts(req)); - db.info(function (err) { - + req.PouchDB.new(name, utils.makeOpts(req)).then(function(db) { + db.info(function (err) { + if (err) { + // temoperary workaround for leveldb adapter, see + // https://github.com/pouchdb/pouchdb/issues/5668 + // when removing this code, also remove mkdir + // from dependencies in package.json + if (err.name === 'OpenError') { + var path = db.__opts.prefix ? db.__opts.prefix + name : name; + + mkdirp(pathResolve(path), function (err) { if (err) { return utils.sendError(res, err, 412); } - utils.setLocation(res, name); - utils.sendJSON(res, 201, {ok: true}); + + db = req.PouchDB.new(name, utils.makeOpts(req)); + db.info(function (err) { + + if (err) { + return utils.sendError(res, err, 412); + } + utils.setLocation(res, name); + utils.sendJSON(res, 201, {ok: true}); + }); }); - }); - return; + return; + } + + return utils.sendError(res, err, 412); } - - return utils.sendError(res, err, 412); - } - utils.setLocation(res, name); - utils.sendJSON(res, 201, {ok: true}); + utils.setLocation(res, name); + utils.sendJSON(res, 201, {ok: true}); + }); }); }); });