Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Sharing user_id value between two MySql table

I'm working on my middleware AuthController.js below. The middleware is part of a CRUD app. I created exports.create which when requested will collect the first name and last name from the CRUD form. Once collected the MySql INSERT query, will insert the data on the MySql user table which is working fine.

What I cannot achieve and I need help is that together with the first name and last name info I want to insert also the variable { user_id: decoded.id }, decoded.id is a variable which take the user_id value from another MySql table called login.

When I request export.create the following error shows on the terminal:

(node:18780) UnhandledPromiseRejectionWarning: TypeError: argument callback must be a function when provided

Basically I want that the value under user_id column from the login table is transferred to the user_id column on user table. Thank for any help.

exports.create = async (req, res, next) => {
    if (req.cookies.jwt) {
        try {
            //1)verify the token
            var decoded = await promisify(jwt.verify)(req.cookies.jwt,
                process.env.JWT_SECRET
            );
            
            // console.log(decoded);
            const { first_name, last_name } = req.body;

            connection.query('INSERT INTO user SET first_name = ?,last_name = ?', { user_id: decoded.id }, [first_name, last_name,], (err, rows) => {
                if (!err) {
                    res.render('add-crew', { alert: 'Crew member added succesfully!' });
                } else {
                    console.log(err);
                }
                console.log('The data from user table:\n', rows);

            });

        } catch (error) {
            console.log(error);
            return next();
        }
    }
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The connection.query function takes in two to three arguments: the SQL statement, (the values) and the callback. Here, the function is taking four arguments, so it thinks [first_name, last_name,] is the callback.

What you can do is:

...
connection.query('INSERT INTO user SET user_id = ?, first_name = ?,last_name = ?', [decoded.id, first_name, last_name,], (err, rows) => {
    if (!err) {
        res.render('add-crew', { alert: 'Crew member added succesfully!' });
    } else {
        console.log(err);
    }
    console.log('The data from user table:\n', rows);
});
...

I hope this is what you are looking for.

Edit you could also do:

...
connection.query('INSERT INTO user SET ?', {user_id: decoded.id, first_name: first_name, last_name: last_name}, (err, rows) => {
    if (!err) {
        res.render('add-crew', { alert: 'Crew member added succesfully!' });
    } else {
        console.log(err);
    }
    console.log('The data from user table:\n', rows);
});
...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda