0

I Added Couchdb Update Function to my Code and is ok But When I use That inside of bot.onText(/^[\/!#]start$/, msg => { I Have This Error:

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

How Should I Solve That? This is My Code:

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);
});
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103

1 Answers1

0

Solved By Myself, You Should Put Update Function Inside of 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);
});
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103