Search code examples
node.jsauthenticationcouchdbcouchdb-nano

CouchDB throws '404 missing' error when inserting a user using nano


It seems CouchDB will randomly throw an error when trying to insert a user.

Here is the full response from CouchDB:

{ [Error: missing]
  name: 'Error',
  scope: 'couch',
  status_code: 404,
  'status-code': 404,
  request:
   { method: 'POST',
     headers:
      { 'content-type': 'application/json',
        accept: 'application/json',
        'X-CouchDB-WWW-Authenticate': 'Cookie',
        cookie: [Object] },
     uri: 'http://127.0.0.1:5984/_users',
     body: '{"_id":"org.couchdb.user:fake_user","name":"fake_user","realname":"fake user","institution":"366a8e5ba861bdd9cad5cd318a002ee4","email":"[email protected]","phone":"123-456-7890","type":"user","roles":[],"password":"123456","level":"user","unconfirmed":"true"}',
     jar: false },
  headers:
   { date: 'Tue, 21 Jan 2014 20:32:23 GMT',
     'content-type': 'application/json',
     'cache-control': 'must-revalidate',
     'status-code': 404,
     uri: 'http://127.0.0.1:5984/_users' },
  errid: 'non_200',
  error: 'not_found',
  reason: 'missing',
  description: 'missing',
  stacktrace:
   [ 'Error: missing',
     '    at Request._callback (/apps/arcapp/node_modules/nano/nano.js:304:39)',
     '    at Request.self.callback (/apps/arcapp/node_modules/nano/node_modules/request/request.js:129:22)',
     '    at Request.EventEmitter.emit (events.js:98:17)',
     '    at Request.<anonymous> (/apps/arcapp/node_modules/nano/node_modules/request/request.js:873:14)',
     '    at Request.EventEmitter.emit (events.js:117:20)',
     '    at IncomingMessage.<anonymous> (/apps/arcapp/node_modules/nano/node_modules/request/request.js:824:12)',
     '    at IncomingMessage.EventEmitter.emit (events.js:117:20)',
     '    at _stream_readable.js:872:14',
     '    at process._tickCallback (node.js:415:13)' ] }

This is obviously not very insightful. All I get is error: not_found, reason: missing, which doesn't tell me much. It only happens sometimes and I can't really tell what's triggering it. Any insight into this is much appreciated.

Here is the relevant code. I'm using nano, but it's just a basic post request to CouchDB.

var user = {
    "_id": "org.couchdb.user:" + username,
    "name": username,
    "realname": first + " " + last,
    "institution": institution,
    "email": email,
    "phone": phone,
    "type": "user",
    "roles": [],
    "password": password,
    "level": "user",
    "unconfirmed": "true",
    "verificationCode": verificationCode
};

nano.request({
    db: '_users',
    method: 'POST',
    body: user
}, function(err, body) {
    if(err) {
        console.log("ERROR INSERTING USER");
        console.log(err);
        res.send(err.status_code + " " + err.reason, err.status_code);
        return;
    }

...

Solution

  • The problem here was that the cookie I was using to validate a user was not being properly unset after logout. So if you logged out and immediately tried to register, the database would authenticate the request as the logged in user. Since only db administrators are allowed access to the _users db, CouchDB would throw a '404 missing' error.

    I was setting nano.config.cookie on login, so when I unset it on logout, it worked!