I am new to node.js (just few days in) and am learning through a tutorial from youtube (don't know if it's allowed to post a link or not).
I am trying to create a database in couchDB. Everything works fine but as soon as I try to enter any details on the form, it displays the exception "Error creating Database Address" (Address is the name of the database I am trying to create)
I followed the tutorial very carefully and have been searching for a solution for a while now but end up with nothing.
Please help if you found something.
App.js:
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var urlencoded = require('url');
var bodyParser = require('body-parser');
var json = require('json');
var logger = require('logger');
var methodOverride = require('method-override');
var nano = require('nano')('http://admin:password@localhost:5984');
var db = nano.use('address');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views',path.join(__dirname,'views'));
app.set('view engine', 'jade');
app.use(bodyParser.json());
// app.use(bodyParser.urlencoded());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/',routes.index);
app.post('/createdb', function(req, res) {
nano.db.create(req.body.dbname, function(err){
if(err) {
res.send("Error creating Database " + req.body.dbname);
return;
}
res.send("Database " + req.body.dbname + "created successfully");
});
});
app.post('/new_contact', function(req, res) {
var name = req.body.name;
var phone = req.body.phone;
db.insert({name : name, phone : phone, crazy : true}, phone, function(err,body, header) {
if(err) {
res.send("Error creating contact");
return;
}
res.send("Contact created successfully");
});
});
app.post('/view_contact', function(req, res) {
var alldoc = "Following are the contacts";
db.get(req.body.phone, {revs_info : true}, function(err, body) {
if(!err) {
console.log(body);
}
if(body) {
alldoc += "Name: " + body.name + "<br/>Phone Number: " + body.phone;
}
else {
alldoc = "No records found";
}
res.send(alldoc);
});
});
app.post('/delete_contact', function(req, res) {
db.get(req.body.phone, {revs_info : true}, function(err, body) {
if(!err) {
db.destroy(req.body.phone, body._rev, function(err, body) {
if(err) {
res.send("error deleting contact");
}
});
res.send("Contacts deleted successfullly");
}
});
});
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port: ' + app.get('port'));
});
index.js
exports.index = function(req,res) {
res.render('index', {title: 'express'});
};
createdb.js
exports.create = function(req, res) {
nano.db.create(req.body.dbname, function() {
if(err) {
res.send("Error creating the Database");
return;
}
res.send("database created successfully");
});
};
index.js and createdb.js both are in routes folder
index.jade
extend layout
block content
h1 Add new Contact
form(method="POST", action = '/new_contact')
p name:
input#title(type = "text", name = "name")
p Phone No.:
input#title(type = "text", name = "phone")
p: button(type = "submit") Add new Contact
h1 Add new Database
form(method = "POST", action= "/createdb")
p Database name:
input#title(type = "text", name = "dbname")
p: button(type="submit") Add new Database
h1 enter Phone number to delete new_contact
form(method = "POST", action= "/delete_contact")
p Phone No.:
input#title(type = "text", name = "phone")
p: button(type="submit") Delete Contact
h1 View Specific contact
form(method = "POST", action= "/view_contact")
p Phone No.:
input#title(type = "text", name = "phone")
p: button(type="submit") Search Contact
layout.jade
doctype html
html
head
title = title
//- link(rel='stylesheet', href='/stylesheet/style.css')
body
block content
layout.jade and index.jade are present in views folder
package.json
{
"name": "sample",
"version": "1.0.0",
"description": "just a sample.",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"errorhandler": "^1.5.1",
"express": "^4.17.1",
"express-session": "^1.17.1",
"jade": "^1.11.0",
"json": "^10.0.0",
"logger": "0.0.1",
"method-override": "^3.0.0",
"nano": "^9.0.3",
"pug": "^3.0.2",
"serve-favicon": "^2.5.0",
"url": "^0.11.0"
}
}
Just briefly skimming over the code, it looks like you're not passing any authentication details to nano:
This might have worked in old versions of CouchDB running in admin party mode, which is not supported anymore from CouchDB 3.0 onwards.
When installing CouchDB locally, you'll need to setup a admin user and password before getting started.
When using nano, you need to provide the credentials like: