I'm creating a REST API, but I'm having difficulty sending a data to the database using 3 modules, let me show my code
rotaCliente.js on this route I get a json with email, name, password and cpf and command for the controller driver module.
const controladorCliente = require('../controladores/controladorCliente');
const db = require('../../config/config.js');
module.exports = function(app){
app.post('/clientes', (req,res) => {
controladorCliente.criarcliente(req.body)
.then(resposta => {
res.send(resposta)
});
});
}
Here is the client controller module ... In the client controller I get all the data in json, and I call a module called UserDao that places this data in the user table and returns me the id of the created user. Then I call the ClientDao module and put this id in the client table, the problem is there, I can not put the data in this table using this division in 3 modules, I could not copy from the user save because it uses only the route that already call DAO directly.
const db = require('../../config/config.js');
const ClienteDao = require('../dao/ClienteDao');
const clienteDao = new UsuarioDao(db.cliente);
const UsuarioDao = require('../dao/UsuarioDao');
const usuarioDao = new UsuarioDao(db.usuario);
module.exports.criarCliente = function(novoCliente){
idUsuario = 0
var usuario = {
"nome" :novoCliente.nome,
"email":novoCliente.email,
"cpf" :novoCliente.cpf,
"senha":novoCliente.senha
}
usuarioDao.criarUsuario(usuario)
.then(resposta => {
idUsuario = resposta[0].id;
});
var cliente = {
"id_usuario": isUsuario
}
return clienteDao.criarCliente(cliente)
.then(resultado =>resultado )
.catch(erro => erro);
}