首页 > 解决方案 > 来自我的本地主机的 Nodejs 表单响应错误

问题描述

我一直在尝试通过位于 localhost:5984 的 apache couchedb 上的 nodejs 将数据存储在我的数据库中因为调用了 else (err) 语句,请帮助我 从我的代码中获取错误消息,下面 的console.log 错误 是我的 app.js 代码

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://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", "ejs");


app.use(bodyparser.json());
app.use(bodyparser.urlencoded());
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 succesfully")
    });
});

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 Succesfully");
    });
});
 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.path;
        }
        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 succesfully");
            }
        });
    });
  http.createServer(app).listen(app.get("port"), function(){
      console.log("Express server listening on port" + app.get("port"));
  });
  

这是我的 json.package

{
  "name": "sesu",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/express": "^4.17.9",
    "@types/logger": "0.0.0",
    "@types/method-override": "0.0.31",
    "body-parser": "^1.19.0",
    "ejs": "^3.1.5",
    "errorhandler": "^1.5.1",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "html": "^1.0.0",
    "jade": "^1.11.0",
    "json": "^10.0.0",
    "logger": "0.0.1",
    "method-override": "^3.0.0",
    "nano": "^9.0.1",
    "pug-cli": "^1.0.0-alpha6",
    "serve-favicon": "^2.5.0",
    "url": "^0.11.0"
  }
}

当然这是我的 HTML 代码


<h1>Add new contact </h1>
<form method="POST" action="/new_contact">
<p>Name:</p>
<input id="title" type="text" name="name"/>
<p>Phone No:</p>
<input id="title" type="text" name="phone"/>
<br>
  <button type="submit">Add new Contact</button>
  <br>
</form>

<h1>Add new Database</h1>
<form method="POST" action="/createdb">
<p>Database Name:</p>
<input id="title" type="text" name="dbname"/>
<br>
  <button type="submit">Add new Database</button>
  </form>
<br>
<h1>Enter phone number to delete new contacts</h1>
<form method="POST" action="/delete_contact">
<p>Phone No:</p>
<input id="title" type="text" name="phone"/>
<br>
  <button type="submit">Delete contact</button>
  </form>
<br>
<h1>View specific contact</h1>
<form method="POST" action="/view_contact">
<p>Phone No:</p>
<input id="title" type="text" name="phone"/>
<br>
  <button type="submit">Search contact</button>
  </form>
<br>

和我的 INDEX.JS 文件分别

exports.index = function(req, res){
    res.sendFile("/Users/LENOVO/node demo/Example/views/index.html");
};

标签: javascripthtmlnode.jsjsonexpress

解决方案


推荐阅读