0
I need to do a query to recover a User’s discounts(idUsuario = user_id) by the Discount models' numbers, however, discount is only related to an idAnuncio(ad_id), so it’s like recovering an id via another id...
The API architecture follows the MVR standard and models and Routes are in the Express structure.
Database is the sqlite.
Follows the model structure:
Follow the get of a Discount, which I need to do by idUsuario(user_id):
module.exports = app => {
const Discount = app.db.models.Discount;
//rota para todos descontos somente de um usuario/fornecedor
app.route("/discounts/provider/:ad_id")
.get((req, res) => {
Discount.findAll({ where: req.params })
.then(result => {
if (result) {
res.json(result);
} else {
res.sendStatus(404);
}
})
.catch(error => {
res.status(412).json({ msg: error.message });
});
})
}
Follow the db.js database file:
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
let db = null;
module.exports = app => {
if (!db) {
const config = app.libs.config;
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
config.params
);
db = {
sequelize,
Sequelize,
models: {}
};
const dir = path.join(__dirname, "models");
fs.readdirSync(dir).forEach(file => {
const modelDir = path.join(dir, file);
const model = sequelize.import(modelDir);
db.models[model.name] = model;
});
Object.keys(db.models).forEach(key => {
db.models[key].associate(db.models);
});
}
return db;
};
What library are you using to manipulate the database? What is the variable
Ads
?– Costamilam
I’m using the sequelize to manipulate the bank and Ads this wrong, it’s actually const Discount = app.db.models.Discount;
– MarcosTrollKs