使用nano插入用户时,CouchDB会抛出'404缺失'错误

时间:2014-01-21 21:20:10

标签: node.js authentication couchdb couchdb-nano

在尝试插入用户时,CouchDB似乎会随机抛出错误。

以下是CouchDB的完整回复:

{ [Error: missing]
  name: 'Error',
  scope: 'couch',
  status_code: 404,
  'status-code': 404,
  request:
   { method: 'POST',
     headers:
      { 'content-type': 'application/json',
        accept: 'application/json',
        'X-CouchDB-WWW-Authenticate': 'Cookie',
        cookie: [Object] },
     uri: 'http://127.0.0.1:5984/_users',
     body: '{"_id":"org.couchdb.user:fake_user","name":"fake_user","realname":"fake user","institution":"366a8e5ba861bdd9cad5cd318a002ee4","email":"fake_user@gmail.com","phone":"123-456-7890","type":"user","roles":[],"password":"123456","level":"user","unconfirmed":"true"}',
     jar: false },
  headers:
   { date: 'Tue, 21 Jan 2014 20:32:23 GMT',
     'content-type': 'application/json',
     'cache-control': 'must-revalidate',
     'status-code': 404,
     uri: 'http://127.0.0.1:5984/_users' },
  errid: 'non_200',
  error: 'not_found',
  reason: 'missing',
  description: 'missing',
  stacktrace:
   [ 'Error: missing',
     '    at Request._callback (/apps/arcapp/node_modules/nano/nano.js:304:39)',
     '    at Request.self.callback (/apps/arcapp/node_modules/nano/node_modules/request/request.js:129:22)',
     '    at Request.EventEmitter.emit (events.js:98:17)',
     '    at Request.<anonymous> (/apps/arcapp/node_modules/nano/node_modules/request/request.js:873:14)',
     '    at Request.EventEmitter.emit (events.js:117:20)',
     '    at IncomingMessage.<anonymous> (/apps/arcapp/node_modules/nano/node_modules/request/request.js:824:12)',
     '    at IncomingMessage.EventEmitter.emit (events.js:117:20)',
     '    at _stream_readable.js:872:14',
     '    at process._tickCallback (node.js:415:13)' ] }

这显然不是很有见地。我得到的只是error: not_found, reason: missing,这并没有告诉我多少。它有时只会发生,我无法确定是什么触发了它。任何对此的见解都非常感激。

这是相关代码。我正在使用nano,但它只是对CouchDB的基本发布请求。

var user = {
    "_id": "org.couchdb.user:" + username,
    "name": username,
    "realname": first + " " + last,
    "institution": institution,
    "email": email,
    "phone": phone,
    "type": "user",
    "roles": [],
    "password": password,
    "level": "user",
    "unconfirmed": "true",
    "verificationCode": verificationCode
};

nano.request({
    db: '_users',
    method: 'POST',
    body: user
}, function(err, body) {
    if(err) {
        console.log("ERROR INSERTING USER");
        console.log(err);
        res.send(err.status_code + " " + err.reason, err.status_code);
        return;
    }

...

2 个答案:

答案 0 :(得分:1)

这里的问题是我用于验证用户的cookie在注销后未正确设置。因此,如果您注销并立即尝试注册,则数据库将以登录用户身份验证请求。由于只允许db管理员访问_users db,因此CouchDB会抛出'404 missing'错误。

我在登录时设置nano.config.cookie,所以当我在注销时取消设置时,它有效!

答案 1 :(得分:0)

这个错误敲响了警钟,但不幸的是,我不是100%如何修复它。

我注意到你有一件事包括&#34;角色:[]&#34;除非你作为管理员发帖,否则我不认为这是允许的。

我没有足够的代表发表评论,但您是否可以发送提出请求的代码?