【问题标题】:Doc not found/missing after being inserted in couchdb database插入 couchdb 数据库后未找到/丢失文档
【发布时间】:2018-06-08 19:56:11
【问题描述】:

我正在尝试创建一个名为“_users”的简单数据库,并使用 Couch-DB 将新用户插入其中。

我在 shell 中使用 Node 来运行以下代码:

UserProfile.js

var nano = require('nano')('http://localhost:5984')        
module.exports = {
    addUser: function(id, name, password){                
        var usersDB = nano.use('_users')
        var options = {
            "_id": "org.couchdb.user:"+id,
            "name": id,
            "roles": [],            
            "type": "user",
            "password": password
        }
        usersDB.insert(options, function(err, body, header) { 
            console.log('insert is being called')
            if (err) {
                console.log(err.message);
                return;
            }            
            console.log(body);
        });

    }
};

节点复制

> var nano = require('nano')('http://localhost:5984')
undefined
> var usersdb = nano.db.destroy('_users')
undefined
> var usersdb = nano.db.create('_users')
undefined
> var profile = require('./UserProfile.js')
undefined
> profile.addUser('cheese', 'flubber', 'goat')
undefined
> insert is being called
> OS process timed out.

运行此程序后,我希望在/_users/cheese 看到一个条目,但不存在任何条目。我做错了什么吗?

【问题讨论】:

  • 1.预期的条目应该是 /_users/org.couchdb.user:cheese 2. usersDB.insert 回调的响应是什么?
  • @AlexisCôté 当我转到/_users/org.couchdb.user:cheese 时,我看到了{"error":"not_found","reason":"missing"}。回调没有响应;正如你在上面的 shell 中看到的,它只是返回 undefined。
  • nano.db.destroynano.db.create 函数使用回调 - API 调用在移动到下一个调用之前解决了吗?

标签: javascript node.js nosql couchdb couchdb-nano


【解决方案1】:

创建用户时,_id 必须是这样的:org.couchdb.user:name

在您的情况下,您创建了一个具有不同名称的用户奶酪。

此外,您可能需要检查错误回调。应该从 Couch 返回一条错误消息。

还有

当你删除和创建数据库时,你必须使用回调。

在创建_users时,即使_users数据库尚未确认创建,也直接创建用户。

【讨论】:

  • 我更新了我的问题以表明我现在拥有具有相同值的 org:couchdb.user:namename (id)。另外,我无法检查错误回调,因为回调无法调用。我刚收到undefined 的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-13
  • 1970-01-01
  • 2018-03-16
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多