简体   繁体   English

使用参数化查询在 postgreSQL 中存储 JSON 个对象

[英]Storing JSON objects in the postgreSQL using parameterized query

I am having problems with saving JSON objects in the database using parameterized queries.我在使用参数化查询在数据库中保存 JSON 个对象时遇到问题。 I am using postman to send this object on red.body enter image description here我正在使用 postman 在 red.body 上发送这个 object 在此处输入图片描述

On my server side I havet his code:在我的服务器端,我有他的代码:

 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.'}, }) })

Why is that I still get this error: enter image description here为什么我仍然收到此错误:在此处输入图片描述

throw new TypeError('Client was passed a null or undefined query')
      ^

TypeError: Client was passed a null or undefined query

Here is my table schema:这是我的表架构:

CREATE TABLE test (
    id serial NOT NULL PRIMARY KEY,
    info json NOT NULL
);

How do I format $1 correctly to get it accept it as JSON file?如何正确格式化 $1 以使其接受为 JSON 文件?

Query example:查询示例:

EXECUTE 'INSERT INTO test (info) VALUES ($1) RETURNING *' USING '[{"size": "Small", "quantity": 1, "product_id": 1}]'::jsonb;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM