我正在使用Stripe处理费用和客户订阅,并且我想将这些处理作为连帽插件使用。连帽 - 更新CouchDB文档(Node.js)
付款和客户的注册和订阅通常出现在Stripe Dashboard中,但我想要做的是更新CouchDB中的_users数据库,以确保将客户的信息保存在某处。 我想要做的是更新org.couchdb.user:user/bill
文件中的stripeCustomerId
字段,该文件来自我的_users
数据库,该数据库在使用连帽衫进行记录时创建。如果可能的话,创建该字段,如果它不存在。
在hoodie-plugin的文档中,更新函数对我来说似乎很模糊。
// update a document in db
db.update(type, id, changed_attrs, callback)
我认为type
是这是CouchDB的文件中提到的一个或一个我们指定当我们添加一个文件与db.add(type, attrs, callback)
例如。
id
似乎是couchdb中的doc id。在我的情况下,它是org.couchdb.user:user/bill
。但我不知道这是我应该通过我的更新功能这个ID。
我认为changed_attrs
是一个Javascript对象,它具有更新或新的属性,但在这里我再次怀疑。
所以我在worker.js
尝试这样:
function handleCustomersCreate(originDb, task) {
var customer = {
card: task.card
};
if (task.plan) {
customer.plan = task.plan;
}
stripe.customers.create(customer, function(error, response) {
var db = hoodie.database(originDb);
var o = {
id: 'bill',
stripeCustomerId: 'updatedId'
};
hoodie.database('_users').update('user', 'bill', o, function(error) {
console.log('Error when updating');
addPaymentCallback(error, originDb, task);
});
db.add('customers.create', {
id: task.id,
stripeType: 'customers.create',
response: response,
}, function(error) {
addPaymentCallback(error, originDb, task);
});
});
}
和其他消息之间,我得到这个错误日志:
TypeError: Converting circular structure to JSON
而我的文件不会被更新:stripeCustomerId
领域保持null
。 我试过JSON.stringify
我的o
对象,但它没有改变一件事。
我希望比你们中的一些人更了解我的这个db.update
函数。