3
I’m starting to study Mean.js and I was in doubt when the best way to create my application constants. I need to store some constants in the project to be accessible in various parts of the code.
Searching, I found nothing related to constants in the documentation of Mean.js.. On the web, I found these references right here in stackoverflow.
https://stackoverflow.com/questions/8595509/how-do-you-share-constants-in-nodejs-modules
Specifically on the second link I found Dominic Barnes' reply very elegant, but in the specific case of Mean.js there is still an abstraction of modules to consider (Mongoose).
How could I create my constants without interfering with the functioning of Mongoose (and without getting ugly, preferably =) )
Note: one thing I would like is that each module file keeps its own constant, so a model of Cars for example, would have the constant
Cars.CARRO_DE_PASEIO
That could have a string, object etc (I’d settle for a string hehe). Something like this...
There it is... with Mongoose I don’t use the module.Xports, but Mongoose.model('Meumodel', Meumodelschema); (Of course I defined Meumodelschema before). Can I, within that model file export the globals as well? Will they be read even if I don’t require the model? I posted these questions, but I’ll do the tests myself later, only right now I’m kind of stuck here.
– Rômulo O. Torres
@Romulus.Torres export to
global
will be accessible. It is not the best practice but it is possible. Perhaps the best is to pass within theMeuModelSchema
. If you put an example of your code/structure it is easier to help.– Sergio
I’m going to give you the example of the own code... see this example of model exported by Mongoose: https://github.com/meanjs/mean/blob/master/app/models/article.server.model.js Now imagine that I go to the controller of Articles and want to use a constant... I wish it was possible for example to have a constant defined as Articles.ARTIGO_A, which had been defined in my model... anyway it is more or less that
– Rômulo O. Torres