Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doc not found/missing after being inserted in couchdb database

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?

like image 765
David J. Avatar asked Dec 28 '17 09:12

David J.


1 Answers

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.

like image 113
Alexis Côté Avatar answered Oct 31 '22 17:10

Alexis Côté