0

我在我的代码中添加了 CouchdbUpdate函数并且没问题但是当我在里面使用它时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);
});
4

1 回答 1

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);
});
于 2017-03-11T06:59:13.340 回答