I'm handling charges and customers' subscriptions with Stripe, and I want to use these handlings as a Hoodie plugin.
Payments and customer's registrations and subscriptions appear normally in Stripe Dashboard, but what I want to do is update my _users database in CouchDB, to make sure customer's information are saved somewhere.
What I want to do is updating the stripeCustomerId
field in org.couchdb.user:user/bill
document, from my _users
database which creates when logging with Hoodie. And if it is possible, to create this field if it does not exist.
In hoodie-plugin's document, the update function seems pretty ambiguous to me.
// update a document in db
db.update(type, id, changed_attrs, callback)
I assume that type
is the one which is mentioned in CouchDB's document, or the one we specify when we add a document with db.add(type, attrs, callback)
for example.
id
seems to be the doc id in couchdb. In my case it is org.couchdb.user:user/bill
. But I'm not sure that it is this id I'm supposed to pass in my update function.
I assume that changed_attrs
is a Javascript object with updated or new attributes in it, but here again I have my doubts.
So I tried this in my 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);
});
});
}
And between other messages, I got this error log:
TypeError: Converting circular structure to JSON
And my file is not updated : stripeCustomerId
field stays null
.
I tried to JSON.stringify
my o
object, but It doesn't change a thing.
I hope than some of you is better informed than I am on this db.update
function.
Finally, I decided to join the Hoodie official IRC channel, and they solved my problem quickly.
Actually user.docs need an extra API, and to update them you have to use hoodie.account
instead of hoodie.database(name)
The full syntax is:
hoodie.account.update('user', user.id, changedAttrs, callback)
where user.id
is actually the account name set in Hoodie sign-up form, and changedAttrs
an actual JS object as I thought.
Kudos to gr2m for the fix; ;)