7

I'm trying to create a simple db called '_users' and insert a new user into it using Couch-DB.

I'm using Node in the shell to run the following code:

UserProfile.js

var nano = require('nano')('http://localhost:5984')        
module.exports = {
    addUser: function(id, name, password){                
        var usersDB = nano.use('_users')
        var options = {
            "_id": "org.couchdb.user:"+id,
            "name": id,
            "roles": [],            
            "type": "user",
            "password": password
        }
        usersDB.insert(options, function(err, body, header) { 
            console.log('insert is being called')
            if (err) {
                console.log(err.message);
                return;
            }            
            console.log(body);
        });

    }
};

node repl

> var nano = require('nano')('http://localhost:5984')
undefined
> var usersdb = nano.db.destroy('_users')
undefined
> var usersdb = nano.db.create('_users')
undefined
> var profile = require('./UserProfile.js')
undefined
> profile.addUser('cheese', 'flubber', 'goat')
undefined
> insert is being called
> OS process timed out.

After running this, I expect to see an entry at /_users/cheese, but no entry exists. Am I doing something incorrectly?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
David J.
  • 1,753
  • 13
  • 47
  • 96
  • 1. The expected entry should be /_users/org.couchdb.user:cheese 2. What is the response from the usersDB.insert callback? – Alexis Côté Dec 28 '17 at 15:19
  • @AlexisCôté When I go to `/_users/org.couchdb.user:cheese` I see `{"error":"not_found","reason":"missing"}`. There is no response from the callback; as you can see in my shell above, it simply returns undefined. – David J. Dec 28 '17 at 15:47
  • The `nano.db.destroy` and `nano.db.create` functions use callbacks - did the API calls resolve before moving to the next call? – tephyr Jan 02 '18 at 20:11

1 Answers1

1

when you create a user, the _id must be like this: org.couchdb.user:name.

In your case, you create a user cheese with a different name.

Also, you might want to check your error callback. An error message should be returned from Couch.

Also

When you delete and create the database, you have to use the callback.

When you create _users, you directly create a user even if the _users database has not been confirmed to be created.

Alexis Côté
  • 3,670
  • 2
  • 14
  • 30
  • I updated my question to show that I now have the `org:couchdb.user:name` and `name` with the same values (`id`). Also, I can't check the error callback because the callback is failing to call. I just get `undefined` returned. – David J. Dec 28 '17 at 16:24