Replies: 1 comment 4 replies
-
Set the values you want to pass on const t = new TransactionModel({
amount: req.body.amount // number, ex: 20$
})
t.$locals.from = req.body.from; // id (user object id)
t.$locals.to = req.body.to; // id (user object id)
await t.save();
// In pre()
TransactionSchema.pre('save', function(){
const doc = this
const fromDoc = await UserMode.findById(doc.$locals.from) // to save the user document itself instead of the reference id
const toDoc = await UserMode.findById(doc.$locals.to) // to save the user document itself instead of the reference id
fromDoc.deductBalance(doc.amount)
toDoc.feedBalance(doc.amount)
}) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I need to pass an argument from the
Model.prototype.create()
method to theSchema.prototype.pre('validate')
method somehow?Is there any way I can do it?
I need for example to do that in multiple places, I need for instance to pass the express request object to Mongoose, I.e.: passing an argument from the controller to the model.
My case:
The problem is that Mongoose is complaining that Casting to Embedded failed.
How to pass an argument from my express controllers to Mongoose (I call it the model because I use MVC) without validations?
*** for reference: here's the UserModel:
Beta Was this translation helpful? Give feedback.
All reactions