I got an error message while creating database in Expressjs

0 votes

https://www.youtube.com/watch?v=JnvKXcSI7yk

I got an error at 1:08:22

Code for creating a database

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


code of app.js

//dependencies we are using

var express = require("express");

var routes = require("./routes");

var http = require("http");

var urlencode = require("url");

var path = require("path");

var bodyParser = require("body-parser");

var json = require("json");

var logger = require("log");

var methodOverride = require("method-override");


var nano = require("nano")("http://localhost:5984"); //address of couchdb server


var db = nano.use("address");


var app = express();


//Environment

app.set("port", process.env.PORT || 3000);

app.set("views", path.join(__dirname, "views"));

app.set("view engine", "jade");


//App using all imported modules

app.use(bodyParser.json());

app.use(bodyParser.urlencoded());

app.use(methodOverride());

app.use(express.static(path.join(__dirname, "public")));


//for creating databases

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");

  });

});


//for new_contact we are creating

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 in creating Contact");

        return;

      }

      res.send("contact created successfully");

    }

  );

});


//To view contact

app.post("/view_contact", function (req, res) {

  var alldocs = "following are the contact";

  db.get(

    req.body.phone,

    {

      revs_info: true,

    },

    function (err, body) {

      if (!err) {

        console.log(body);

      }

      if (body) {

        alldocs += "Name:" + body.name + "<br/>Phon Number:" + body.phone;

      } else {

        alldocs = "No records found";

      }

      res.send(alldocs);

    }

  );

});

//to delete contact

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("contact deleted successfully");

      }

    }

  );

});


//creating server

http.createServer(app).listen(app.get("port"), function () {

  console.log("Express server listening on port " + app.get("port"));

});

please help as I'm new to nodejs and expressjs.

Dec 17, 2020 in Node-js by nishant kumar
• 120 points
781 views

Hi, @Nishant,

Could you please post your error logs here? 

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Node-js

0 votes
1 answer

How do I redirect in expressjs while passing some context?

Hello @kartik, The easiest way I have found ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,840 points
23,899 views
0 votes
1 answer

How to write a test which expects an Error to be thrown in Jasmine?

Hello @kartik, Try using an anonymous function instead: expect( ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
9,514 views
0 votes
1 answer

How do I perform an id array query in Mongoose?

Hello @kartik, Here is a mongoosey way to ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,840 points
6,249 views
0 votes
1 answer

Installing angularjs with NPM and NODEJS brings an error in ubuntu 16.04

Rename your angular folder to angular-test or ...READ MORE

answered May 30, 2022 in Node-js by Vaani
• 7,070 points
1,309 views
0 votes
1 answer

How do I “include” functions from my other files in nodejs?

Hello @kartik, You require any js file,so you just ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,840 points
3,421 views
0 votes
1 answer

Error: listen EADDRINUSE while using nodejs?

Hello @kartik, EADDRINUSE means that the port number which listen() tries ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,840 points
6,550 views
+1 vote
0 answers

Metamask popup on Server side (Express js) ?

So I have this problem when i ...READ MORE

Jul 15, 2019 in Blockchain by Dzaky
• 130 points
1,565 views
0 votes
1 answer

how to safely deploy npm install without it causing inconsistencies?

The recent versions on npm generates a ...READ MORE

answered Apr 11, 2018 in DevOps on Cloud by DareDev
• 6,890 points
980 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP