Tengo problemas para guardar objetos JSON en la base de datos mediante consultas parametrizadas. Estoy usando cartero para enviar este objeto en red.body ingrese la descripción de la imagen aquí
En mi lado del servidor tengo su código:
queryController.createQuery = (req, res, next) => { const { info }= req.body; const sqlQuery = 'INSERT INTO test (info) VALUES ($1) RETURNING *'; db.query(sqlQuery, [info]) .then(result => { console.log(result); if (result) res.locals.message = "Saved the query successfully"; next(); }) .catch(err => { return next({ log: `queryController.createQuery: ERROR: ${typeof err === 'object' ? JSON.stringify(err) : err}`, message: { err: 'Error occurred in queryController.createQuery. Check server log for more details.'}, }) })¿Por qué sigo recibiendo este error? Ingrese la descripción de la imagen aquí .
throw new TypeError('Client was passed a null or undefined query') ^ TypeError: Client was passed a null or undefined queryAquí está mi esquema de tabla:
CREATE TABLE test ( id serial NOT NULL PRIMARY KEY, info json NOT NULL );¿Cómo formateo $ 1 correctamente para que lo acepte como archivo JSON?
Ejemplo de consulta:
EXECUTE 'INSERT INTO test (info) VALUES ($1) RETURNING *' USING '[{"size": "Small", "quantity": 1, "product_id": 1}]'::jsonb;