微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

couchDB 无法使用 node.js

如何解决couchDB 无法使用 node.js

我是 node.js 的新手(才几天),正在通过 youtube 上的教程学习(不知道是否允许发布链接)。

我正在尝试在 couchDB 中创建一个数据库。一切正常,但只要我尝试在表单上输入任何详细信息,它就会显示异常“创建数据库地址时出错”(地址是我尝试创建的数据库名称

我非常仔细地遵循了教程,并且一直在寻找解决方案一段时间,但最终一无所获。

如果你发现了什么,请帮忙。

Here is the image of the local host

Here is the image of the error

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',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',res) {
    var alldoc = "Following are the contacts";
    db.get(req.body.phone,{revs_info : true},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',res) {
    db.get(req.body.phone,body) {
        if(!err) {
            db.destroy(req.body.phone,body._rev,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,function() {
        if(err) {
            res.send("Error creating the Database");
            return;
        }
        res.send("database created successfully");
    });
};

index.js 和 createdb.js 都在路由文件夹中

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.jadeindex.jade 存在于视图文件夹中

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"
  }
}

解决方法

简单浏览一下代码,您似乎没有将任何身份验证详细信息传递给 nano:

var nano = require('nano')('http://localhost:5984');
var db = nano.use('address');

/* ... */

app.post('/createdb',function(req,res) {
    nano.db.create(req.body.dbname,function(err){

这可能适用于以管理员组模式运行的旧版本 CouchDB,从 CouchDB 3.0 开始不再支持该模式。

在本地安装 CouchDB 时,您需要在开始之前设置管理员用户和密码。

使用 nano 时,您需要提供以下凭据:

var nano = require('nano')('http://admin:password@localhost:5984')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。