连帽衫 – 更新一个CouchDB文档(Node.js)
我正在使用Stripe处理费用和客户订阅,我想用这些处理作为连帽插件。
付款和客户的注册和订阅通常出现在Stripe Dashboard中,但我想要做的是更新CouchDB中的_users数据库,以确保客户的信息保存在某个地方。 我想要做的就是更新stripeCustomerId
中的stripeCustomerId
字段org.couchdb.user:user/bill
document,从我的_users
数据库中创build, _users
数据库是用Hoodie进行login时创build的。 如果可能的话,创build这个字段,如果它不存在。
在连帽插件的文档中,更新函数对我来说似乎很模糊。
// update a document in db db.update(type, id, changed_attrs, callback)
我假设这个type
是CouchDB文档中提到的type
,或者是我们用db.add(type, attrs, callback)
添加文档时指定的db.add(type, attrs, callback)
。
id
似乎是couchdb中的文档ID。 在我的情况下,它是org.couchdb.user:user/bill
。 但我不确定这是我应该通过我的更新function这个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
函数。
最后,我决定joinHoodie官方的IRC频道,他们很快就解决了我的问题。
其实user.docs需要一个额外的API,并更新它们,你必须使用hoodie.account
而不是hoodie.database(name)
完整的语法是:
hoodie.account.update('user', user.id, changedAttrs, callback)
其中user.id
实际上是在Hoodieregistry单中设置的帐户名称,并且像我想的那样changedAttrs
了一个实际的JS对象。
荣誉gr2m的修复; ;)