Couchdb“nano”模块 - 未处理的拒绝 - 不是Object的功能

时间:2017-03-09 07:18:03

标签: javascript node.js couchdb couchdb-nano

我在我的代码中添加了Couchdb Update函数并且没问题但是当我在bot.onText(/^[\/!#]start$/, msg => {内部使用它时出现此错误:

Unhandled rejection TypeError: alice.update is not a function
    at Object.bot.onText.msg [as callback] 

我该如何解决? 这是我的代码:

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() {
  var alice = nano.use('alice');
                                        /////   update Function
 alice.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);
 });
};

  });
});



bot.onText(/^[\/!#]start$/, msg => {
                                    /////  using update 
   var alice = nano.use('alice');
 alice.update({ crazay: true }, 'rabbit', function(err, body, header) {
      if (err) {
        console.log('[alice.insert] ', err.message);
        return;
      }
      console.log('you have updated the rabbit.')
      console.log(body);
    });
 const opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: [['TEST']],
      resize_keyboard:true,
      one_time_keyboard: true
    })
  };
  bot.sendMessage(msg.chat.id, `Stored In DB`, opts);
});

1 个答案:

答案 0 :(得分:0)

我自己解决,你应该把更新功能放在bot.onText

里面
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() {
  var alice = nano.use('alice');
  });
});



bot.onText(/^[\/!#]start$/, msg => {
                                    /////  using update 
   var alice = nano.use('alice');
                                        /////   update Function
 alice.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);
 });
};

 alice.update({ crazay: true }, 'rabbit', function(err, body, header) {
      if (err) {
        console.log('[alice.insert] ', err.message);
        return;
      }
      console.log('you have updated the rabbit.')
      console.log(body);
    });
 const opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: [['TEST']],
      resize_keyboard:true,
      one_time_keyboard: true
    })
  };
  bot.sendMessage(msg.chat.id, `Stored In DB`, opts);
});