2017-03-08 61 views
0

我正在使用此Node.js模块nanoCouchDB无法通过nano模块更新文档

为什么我无法更新我的文档?我会想疯狂:真实,然后再次虚假。
这是我的代码:

var nano = require('nano')('http://localhost:5984'); 

// clean up the database we created previously 
nano.db.destroy('alice', function() { 
    // create a new database 
    nano.db.create('alice', function() { 
    // specify the database we are going to use 
    var alice = nano.use('alice'); 
    // and insert a document in it 
    alice.insert({ crazy: true }, 'rabbit', function(err, body, header) { 
     if (err) { 
     console.log('[alice.insert] ', err.message); 
     return; 
     } 
     console.log('you have inserted the rabbit.') 
     console.log(body); 
    }); 
    }); 
}); 

回答

0

纳米没有附带默认的更新方法。这就是为什么我们需要定义一个可以为我们做到的自定义方法。在数据库连接代码之后,在app.js文件的顶部附近声明以下内容。

test_db.update = function(obj, key, callback){ 
var db = this; 
db.get(key, function (error, existing){ 
    if(!error) obj._rev = existing._rev; 
    db.insert(obj, key, callback); 
}); 
} 

然后,您可以使用update方法在代码:

// and update a document in it 
    alice.update({ crazy: false }, 'rabbit', function(err, body, header) { 
     if (err) { 
     console.log('[alice.insert] ', err.message); 
     return; 
     } 
     console.log('you have updated the rabbit.') 
     console.log(body); 
    }); 
    }); 
});