2017-03-09 26 views
0

我說CouchDB的Update功能到我的代碼的功能,並處於正常,但是當我使用的這裏面bot.onText(/^[\/!#]start$/, msg => {我有這樣的錯誤:CouchDB的`nano`模塊 - 未處理的拒絕 - 是不是在對象

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); 
}); 

回答

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); 
}); 
相關問題