0

I've been trying to use console.log's with ther errors and can't figure it out. I have a form to create an account and upload an image in the process. To send the data to sql i have an if(!req.files){}, and to upload the image I have an if(req.files){}.

I'm getting the error: "TypeError: Cannot convert object to primitive value" in the uploading image part as I made console.log of the other information and it's

PS: I'm using the Post method for submiting the form

this is the snip of the node.js code:

        //The code before saved a row of data in sql
        //The atribute of the path has been filled with something for it to be updated

        //UPDATE the atribute that saves the path for the image
        //Save the image in the chosen file

        else if (req.files.fotografia_funcionario) {
            //novo query para fazer o update do atributo
            console.log("Estamos no " + req.files);
            console.log("Estamos no req.files");
            query += "UPDATE Funcionarios SET fotografia_funcionario = " + req.files.fotografia_funcionario + ";";


            pool.query(query, function (err, result, fields) {
                var html = ""; 
                html += head;
                html += "<h2>Regista um funcionário</h2>\n";
                if (!err) {
                    if (result[0] && result[0].affectedRows > 0) {
                        html += "<p>O funcionário'" + req.body.nome_funcionario + "' foi inserido com sucesso</p>\n";
                    }
                    else {
                        html += "<p>Não foi possivel inserir o funcionário '" + req.body.nome_funcionario + "'</p>\n";
                        html += "<p>Não foi possivel inserir a fotografia de'" + req.body.nome_funcionario + "'</p>\n";
                    }
                    if (result[1] && result[1].affectedRows > 0) {
                        html += "<p>A fotografia de'" + req.body.nome_funcionario + "' foi inserida com sucesso</p>\n";
                    }
                    else {
                        html += "<p>Não foi possivel inserir a  fotografia de '" + req.body.nome_funcionario + "'</p>\n";
                    }
                    req.files.fotografia_funcionario.mv("public/fotografias_funcionarios/" + req.files.fotografia_funcionario, function (err) {
                        if (err) {
                            console.error(err);
                            console.error("Erro ao guardar a imagem no ficheiro");
                        }
                    });
                }

I'm trying to save the image and its name The form works if I sont use the upload of the image

Perdix
  • 1
  • 1

0 Answers0