Only recently began to learn node js. Help solve this problem: For example, there are currency tables and wallets, for the wallet there is a field with the types "Currency". If you need to edit the wallet, you could select data in the currency field only from the "Currency" table. How to do it in general, I understand (find all currencies Currency.find ({}, ...) and transfer to ejs). The problem is that there are many such tables and fields, so I would like to know how this is done correctly? This is how I’ve done it now, but it seems to me that the approach itself is not the right one, plus I don’t know how in Middleware - selectInputs to transfer a variable with the settings I need for each "addPurse" page:
var mongoose = require('mongoose'); var schemaCurrency = new mongoose.Schema({ name: String, code: String, course: Number }); module.exports = mongoose.model("Currency", schemaCurrency); Table wallets:
var mongoose = require('mongoose'); var schemaPurse = new mongoose.Schema({ name: String, currency: { id: { type: mongoose.Schema.Types.Object, ref: "Currency" }, name: String }, comment: String }); module.exports = mongoose.model("Purse", schemaPurse); When you go to the page with the addition of the wallet:
router.get('/purses/add', selectInputs, function(req,res,next){ ... }); Middleware - selectInputs
module.exports = function(req, res, next){ var currentPage = require('./config.json')["addPurse"]; if(!currentPage){return} var inputs = currentPage.inputs; inputs.forEach(function(input){ var model = require(input.model); model.find({}, function(err, allData){ if(err){ next(err); } else { res.locals[input.name] = allData; } }); }); next(); } ./config.json
{ "addPurse":{ "inputs": [ {"name":"currencies", "model":"../../models/currency"} ] } } currency field in add.ejs
<div class="form-group"> <select class="form-control" name="currency" required> <% currencies.forEach(function(currency){ %> <option value="<%= currency._id %>"><%= currency.name %></option> <% }) %> </select> </div>