Couchdb `nano` 模块 - 未处理的拒绝 - 不是 Object 的函数
Couchdb `nano` module - Unhandled rejection - is not a function at Object
我在我的代码中添加了 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);
});
我自己解决的,你应该把更新功能放在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);
});
我在我的代码中添加了 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);
});
我自己解决的,你应该把更新功能放在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);
});