0
I am trying to register a JSON String in Mysql, I get an array with URL’s after a .map
on the return of a request to upload multiple images to AWS S3, I use JSON.stringify to leave the array in String format to upload, when I give a console.log it appears the right way I hope:
["https://somecluster.s3.sa-east-1.amazonaws.com/somekey.jpg","https://somecluster.s3.sa-east-1.amazonaws.com/somekey.jpg"]
but when I enter the Mysql Insert command returns me with 3 backslashes escaping double quotes like this, I will go here the error returned in Inuit:
{ "error": { "code": "ER_PARSE_ERROR", "errno": 1064, "sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO imagens (id_produto_fk, imagens_url) VALUES (LAST_INSERT_ID(), '[\\\"h' at line 1", "sqlState": "42000", "index": 0, "sql": "INSERT INTO produtos (nome, preco) VALUES ('Iphone 12 xr', '3000.00'); INSERT INTO imagens (id_produto_fk, imagens_url) VALUES (LAST_INSERT_ID(), '[\\\"https://somecluster.s3.sa-east-1.amazonaws.com/somekey.jpg\\\",\\\"https://somecluster.s3.sa-east-1.amazonaws.com/somekey.jpg\\\"]');" } }
as it is not something directly related to Stringify I do not know how to solve and I can only imagine that it has to do with the definitions of urlencoded or something like that, I will leave down here the useful parts that can be responsible for the problem.
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Header',
'Content-Type, Origin, X-Requested-With, Accept, Authorization'
);
if (req.method === 'OPTIONS') {
res.header('Acess-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200)
}
next();
})```
```const router = require('express').Router();
const mysql = require('../mysql').pool;
const multer = require('multer');
const multerConfig = require('../multer');
// cadastra um produto com até 5 imagens
router.post("/", multer(multerConfig).array('imagem', 5), async (req, res) => {
const locations = req.files.map(function(file) {
return file.location;
}); ```
mysql.getConnection((error, conn) => {
const locations = req.files.map(function(file) {
return file.location; // or file.originalname
});
const myJson = JSON.stringify(locations);
console.log(myJson);
// retorna os erros no acesso da rota
if (error) { return res.status(500).send({ error: error }); }
conn.query(
'INSERT INTO produtos (nome, preco) VALUES (?, ?); INSERT INTO imagens (id_produto_fk, imagens_url) VALUES (LAST_INSERT_ID(), ?);',
[req.body.nome, req.body.preco, myJson],
(error, resultado, field) => {
// libera a conexão apos realizar a query
conn.release();
// retorna erros no cadastro
if (error) { return res.status(500).send({ error: error }); }
// retorna o id do produto caso seja cadastrado
res.status(201).send({
mensagem: 'Produto cadastrado com sucesso',
id_produto: resultado.insertId
});
});
});
});