我正在使用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
函數。