简体   繁体   English

如何使用邮递员 JSON 插入 postgres 的数组列?

[英]How to INSERT INTO an array column of postgres using postman JSON?

I have a table with an array column myarray text[] .我有一个带有数组列myarray text[] I have created an endpoint for inserting into array我创建了一个用于插入数组的端点

exports.createVaraus = async (id, myarray) =>$    try {
        const result = await client.query('INSERT INTO mytable (id, myarray) VALUES ($1, $2) RETURNING id', 
 [id, myarray]
        );
        return result.rows[0]
    } catch (err) {
        throw new Error(err);
    }
}

Now when I do POST with postman with following JSON:现在,当我使用以下 JSON 与邮递员进行 POST 时:

{
    "id": 1,
    "myarray": {"column1": "test"}
}

I get the following error:我收到以下错误:

"message": "error: malformed array literal: \\"{\\"column1\\":\\"test\\"}\\"" "message": "错误:格式错误的数组文字:\\"{\\"column1\\":\\"test\\"}\\""

What is wrong and how to do this properly?有什么问题以及如何正确执行此操作?

My table holds 2 columns: id and myarray.我的表包含 2 列:id 和 myarray。

I am using express.我正在使用快递。

My request body:我的请求正文:

  exports.updateVaraus = async(request, response) => {
    try {
      let {id} = request.params;
      const varausObject = request.body;
      const updatedVaraus = await varausModel.updateVaraus(varausObject, id);
      response.status(200).send(updatedVaraus);
    } catch (err) {
      response.status(400).send({message: err.message});
    }
  }

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

相关问题 如何使用 Bookshelf/Knex 在 Postgres 中插入/更新“ARRAY”列 - How to insert/update `ARRAY` column in Postgres using Bookshelf/Knex Postgres & Javascript - 如何在 Nodejs 中格式化 object 数组以插入 Postgres JSON 列? - Postgres & Javascript - How to format object array in Nodejs to be inserted into Postgres JSON column? 如何使用Mongoose在MongoDB中插入JSON数组 - How to insert an array of JSON in MongoDB using Mongoose 如何使用 knex/postgres 创建可以存储对象数组的列 - How to create a column that can store an array of objects using knex/postgres 如何使用 Node.js 将特定键:Json 的值插入到 postgres 表中 - How do I insert a specific key: value of Json into a postgres table using Node.js 尝试使用 json_to_recordset() 插入 JSON 数组时出现“列不存在” - 'Column Does Not Exist' when trying to INSERT JSON Array using json_to_recordset() 如何使用 node.js 将 json 数组插入到 mongodb? - How to insert json array to mongodb using node.js? 如何使用JavaScript ES6将数组插入到扳手数据库中的列中 - How to insert array into a column in spanner database using javascript es6 使用带有数组 map 的 Async/Await 将数据插入到 postgres 表中 - Using Async/Await with array map to insert data into a postgres table JSON Postman正文不适用于PostgreSQL插入 - JSON Postman body not working for PostgreSQL insert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM