Send a data to the database with Node js

Asked

Viewed 44 times

0

I am creating a REST API, but with difficulty in sending a data to the database using 3 modules, let me show my code

rotaCliente.js on this route I receive a json with email,name,password and Cpf and send to the controller 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 take all the data in json, and I call a module called Usuariodao that puts this data in the user table and returns me the id of the created user. Then I call the module Clientedao and put this id in the table client, 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 calls the direct DAO.

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);              
} 
  • Don’t get it, is the problem saving customers or users? Show any error in the console? Are you sure that resposta[0].id returns the correct value?

  • the problem is in saving clients, does not show error in the console, says only that failed to do the post method, and yes answer[0]. id returns the id.Thanks

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.