CouchDb未处理的“错误”事件-Node js [英] CouchDb unhandled 'error' event - Node js
问题描述
我是Node和CouchDb的新手,我正在尝试入门。
我正在努力编写一段代码来工作。
我想创建一个表 users
,插入一个新用户并同时获得另一个用户。
I'm new to Node and CouchDb and I'm trying to get my hands on it.
I'm struggling to make a piece of code to work.
I would like to create a table users
, insert a new user and 'at the same time' getting another user.
启动 node app.js
时出现此错误:
antoine@ubuntu:~/projects/couchDb$ node app.js
Database users exists.
{"error":"conflict","reason":"Document update conflict."}
Leaving saveDoc
events.js:48
throw arguments[1]; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1107:15)
at Socket.onend (http.js:1188:27)
at TCP.onread (net.js:369:26)
这是我的代码,有什么问题吗?
(当我删除getDoc函数时,错误消失了)
我正在使用couchDB 1.0.1和节点0.6.12
And here is my very code, is there something wrong? (When I remove the getDoc function the error goes away) I'm using couchDB 1.0.1 and node 0.6.12
code> jdoe4 和 jdoe
文档已经存在于 users
数据库中。
The jdoe4
and jdoe
documents are already present in the users
database.
var dbHost = "127.0.0.1";
var dbPort = 5984;
var dbName = 'users';
var couchdb = require('felix-couchdb');
var client = couchdb.createClient(dbPort, dbHost);
var user = {
name: {
first: 'John',
last: 'Doe'
}
}
var db = client.db(dbName);
db.exists(function(err, exists) {
if (!exists) {
db.create();
console.log('Database ' + dbName + ' created.');
} else {
console.log('Database ' + dbName + ' exists.');
}
db.saveDoc('jdoe4', user, function(err, doc) {
if( err) {
console.log(JSON.stringify(err));
} else {
console.log('Saved user.');
}
console.log('Leaving saveDoc');
});
db.getDoc('jdoe', function(err,doc) {
if( err) {
console.log(JSON.stringify(err));
} else {
console.log(JSON.stringify(doc));
}
console.log('Leaving getDoc');
});
});
推荐答案
我很快意识到felix-couchdb与节点8(我知道您没有使用版本8,但是有一天会使用),所以我切换到了nano Sofadb,这是以下代码:
I quickly realized that felix-couchdb is not compatible with node 8 (I know you're not using version 8, but you will someday), so I switched to nano couchdb and here's the following code:
- 它将检查是否已创建数据库
- 仅在给定键唯一的情况下才会插入
-
它将获得具有密钥的用户
- It will check if the db is created
- It will insert only if the key given is unique
It will get the user with a key
var couchdb = require('nano')('http://localhost:5984')
, dbName = 'users'
, db = couchdb.use(dbName)
;
var user = {
name: {
first: 'John',
last: 'Doe'
}
}
couchdb.db.create(dbName, function(err, db_body) {
db.insert(user, 'jdoe4', function(err, doc, header) {
if( err) {
console.log('Cannot save user');
} else {
console.log('Saved user.');
}
console.log('Leaving saveDoc');
});
db.get('jdoe', function(err, doc) {
if( err) {
console.log('Cannot get user');
} else {
console.log(JSON.stringify(doc));
}
console.log('Leaving getDoc');
});
});
值得注意的是,会在相同时间获得它们,它仍然向数据库发出3个请求,1个检查它是否存在,1个插入(或不插入),1个获得。
One thing worth noting is that, while this will get them at the "same time," it's still making 3 requests to the db, 1 to check if it exists, 1 to insert (or not), 1 to get.
这篇关于CouchDb未处理的“错误”事件-Node js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!