简体   繁体   English

插入couchdb数据库后找不到/缺少文档

[英]Doc not found/missing after being inserted in couchdb database

I'm trying to create a simple db called '_users' and insert a new user into it using Couch-DB. 我正在尝试创建一个名为'_users'的简单数据库,并使用Couch-DB将新用户插入其中。

I'm using Node in the shell to run the following code: 我在shell中使用Node来运行以下代码:

UserProfile.js 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);
        });

    }
};

node repl 节点repl

> 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.

After running this, I expect to see an entry at /_users/cheese , but no entry exists. 运行之后,我希望在/_users/cheese看到一个条目,但不存在任何条目。 Am I doing something incorrectly? 我做错了什么吗?

when you create a user, the _id must be like this: org.couchdb.user:name . 创建用户时,_id必须如下所示: org.couchdb.user:name

In your case, you create a user cheese with a different name. 在您的情况下,您创建一个具有不同名称的用户奶酪。

Also, you might want to check your error callback. 此外,您可能想要检查错误回调。 An error message should be returned from Couch. 应从Couch返回错误消息。

Also

When you delete and create the database, you have to use the callback. 删除并创建数据库时,必须使用回调。

When you create _users, you directly create a user even if the _users database has not been confirmed to be created. 创建_users时,即使尚未确认创建_users数据库,也直接创建用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM