Doc not found/missing after being inserted in couchdb database
Asked Answered
M

1

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?

Manno answered 28/12, 2017 at 9:5 Comment(3)
1. The expected entry should be /_users/org.couchdb.user:cheese 2. What is the response from the usersDB.insert callback?Electrocautery
@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.Manno
The nano.db.destroy and nano.db.create functions use callbacks - did the API calls resolve before moving to the next call?Hibernate
B
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.

Bound answered 28/12, 2017 at 16:2 Comment(1)
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.Manno

© 2022 - 2024 — McMap. All rights reserved.