These are chat archives for Automattic/mongoose

17th
Dec 2015
Joseph Thibeault
@jrthib
Dec 17 2015 17:00
do you know if you can access another model within a validator?
in order to validate something I need to know if the value was whitelisted in another document
maybe i should be using a pre-save hook instead?
James Lee
@uptownhr
Dec 17 2015 17:28
@jrthib as long as you require the model you can access
P free save would work
Joseph Thibeault
@jrthib
Dec 17 2015 17:43
hmm
requiring the model like using this.model(‘MyModel’) or like the actual model file
when doing this.model it throws a schema not registered error
MissingSchemaError*
Valeri Karpov
@vkarpov15
Dec 17 2015 17:45
@jrthib can always just do mongoose.model('MyOtherModel'); . I would typically just register my schemas in a dependency injector to avoid cycles and scope dependencies, but that works too
Joseph Thibeault
@jrthib
Dec 17 2015 17:46
i tried mongoose.model(‘MyModel’) within the validator as well and it returned the missing schema error
also, good point about the scope dependencies
Valeri Karpov
@vkarpov15
Dec 17 2015 17:47
@FongZooZ I'm actually working a lot with that right now :) long story short, the best way is to have an array like this: delta: [{ path: String, newValue: Mixed }] and just stuff any changes in there. Then, to apply the changes, just loop through the array and do doc.set(delta.path, delta.newValue);
@jrthib how are you registering your model? That should work...
Joseph Thibeault
@jrthib
Dec 17 2015 17:48
just for sanity, let me try one more time then i’ll answer that question :P
same thing: Schema hasn't been registered for model
and i’m doing the mongoose.model accessor within the validator function
our models are registered after the db connection is made
in this fashion mongoose.model('NotificationLog', require('./notifications').NotificationLogSchema);
Valeri Karpov
@vkarpov15
Dec 17 2015 17:52
hmm then mongoose.model('NotificationLog'); should get you the model...
Joseph Thibeault
@jrthib
Dec 17 2015 17:53
hmm
do you have an example of a well structured application that registers it’s models inline with what you would call “best practices"
i want to compare. maybe i have it doing weird stuff
hahaha, actually, it’s a typo that did me in
it is now working as expected
thanks @vkarpov15 and @uptownhr for the help
Valeri Karpov
@vkarpov15
Dec 17 2015 18:00
not yet but working on it :) not sure if I'll have it out this year, but early next year
long story short, I use https://github.com/vkarpov15/wagner-core for everything mongoose-related
I'm deathly allergic to singletons
Joseph Thibeault
@jrthib
Dec 17 2015 18:23
haha
i’ll check it out