Skip to content
This repository has been archived by the owner on Aug 20, 2023. It is now read-only.

Question #5

Closed
eric-hc opened this issue Feb 13, 2020 · 2 comments
Closed

Question #5

eric-hc opened this issue Feb 13, 2020 · 2 comments
Labels
question Further information is requested

Comments

@eric-hc
Copy link

eric-hc commented Feb 13, 2020

Is there a way to interact with the db outside of koa's ctx? For instance, I want to do something like

var query = {'username': req.user.username};
req.newData.username = req.user.username;

MyModel.findOneAndUpdate(query, req.newData, {upsert: true}, function(err, doc) {
    if (err) return res.send(500, {error: err});
    return res.send('Succesfully saved.');
});

...but I do not have access to ctx.model where I want to update this. Any ideas?

@South-Paw South-Paw added the question Further information is requested label Feb 13, 2020
@South-Paw
Copy link
Owner

South-Paw commented Feb 13, 2020

This is koa-middleware, so by nature you have to have the ctx to interact with it.

If you're wanting to interact with the database outside of the ctx, then I don't think this package is the right solution for that use case (I also wouldn't recommend choosing to pass the ctx around everywhere its needed either)

Unfortunately, I don't have an open source example of how I abstracted this package into it's own files in my project so that it could be used this way.

@eric-hc
Copy link
Author

eric-hc commented Feb 13, 2020

Definitely makes sense, thanks!

@eric-hc eric-hc closed this as completed Feb 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants