Save/Update DB in one class and use ( err, res ) in other class
davidfloegel opened this issue · comments
Hi,
great library!
I was wondering if what I want can be realised.
I have a class where I want to update a schema document.
update( id, data ) {
model.findById( id, ( err, doc ) => {
// set data
// save
// return new
} )
}
Now in my controller I want to be able to just do this:
actionUpdate() {
const update = myclass.update( 123, { title: 'Test' } )
// check for error
// return new object
}
I wonder if something like this is possible?
Help appreciated :)
Thanks
You'll have to give me a little bit more of context. I don't see how you're trying to connect this module with your database. This is just about providing a wrapper so you can write co
-like code in your express middlewares.
Sorry, I explained extremely bad haha.
So basically, this is what I got:
I have a repository class for my Mongoose Schema to reduce the amount of code-redundancy.
One of them looks like this. It works perfectly fine too:
class EventRepository {
*update( data ) {
// [ ...some validation here...]
// update and return
return yield Event.findOneAndUpdate( { _id: data.event }, { $set: data.data }, { new: true } )
}
}
Now, the actual callback for the method findOneAndUpdate
has an err
and updated
property:
findOneAndUpdate( id, { $set: data }, { new: true }, ( err, newDoc ) => {
if( err ) {
res.status( 500 ).send( err )
}
res.status( 200 ).send( newDoc )
} )
My question is, can I somehow get access to the err
and newDoc
property when using yield? So for example:
const updated = yield EventRepository.save( data )
// now here check for errors:
if (updated.error) {
// send error
}
// do something with the updated object
Does that make more sense?
If EventRepository.save
returns a yieldable when passing data
, you can do that inside co
just fine. It looks like you'd be nesting yieldables, which is perfectly supported in co
, therefore supported by this library.
Something like this should work for you:
app.put('/stuff', wrap(function* (req, res) {
const er = new EventRepository();
const updated = yield er.update({ some: 'data' });
res.send(updated);
})
Hi! Thanks for getting back to me :)
Yes what you mentioned works, but how to I check for an error?
So if er.update()
fails I would like to handle that rather then just sending whatever updated
contains..
Do you know what I mean?
If that fails you can simply try-catch. See examples at co
On Wed, Nov 23, 2016, 12:03 David Floegel notifications@github.com wrote:
Hi! Thanks for getting back to me :)
Yes what you mentioned works, but how to I check for an error?
So if er.update() fails I would like to handle that rather then just
sending whatever updated contains..Do you know what I mean?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#15 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAYopbSsIn7-2LI3SiiKo5PHrlZAjTa9ks5rBFXcgaJpZM4K4V_z
.
Closing due to inactivity