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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With