javascript+mariadb:sql查询返回json对象数组并从响应中删除“\`正斜杠

m3eecexj  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(220)

我在javascript函数中将一个原始查询应用到我的sql(mariadb)表中。
::更新问题:::
下面是一个javascript函数,用于创建一个sql查询,在其中我还尝试了 JSON_ARRAY 以及 JSON_OBJECT 答案中建议的方法。

public async getProducts() {
  try {
  let productTbl = '`products`';
  let prodImgTbl = '`product_images_ids`';
  let productImg = '`product_images`';

  let query = `SELECT 
  ${productTbl}.id AS productId,
  GROUP_CONCAT(JSON_ARRAY(JSON_OBJECT(
    "imageId", ${prodImgTbl}.id, 
    "imageUrl", ${productImg}.thumbnail_image
  ))) imageData,

  FROM ${productTbl} 
  JOIN ${prodImgTbl} ON ${prodImgTbl}.product_id = ${productTbl}.id
  JOIN ${productImg} ON ${productImg}.id = ${prodImgTbl}.product_image_id
  GROUP BY ${productTbl}.id `

  let records = await pool.query(query);
  return records
  } catch (error) {
  throw error;
  }
}

使用这个查询,我得到的结果在 Postman 如下,这是添加 \ 每一个键和值。

this.productList = [
  {
    "productId": 3,
    "imageData": "[{\"imageId\": 17, \"imageUrl\": \"/assets/images/321/1/1/1/image.jpg\"}],[{\"imageId\": 18, \"imageUrl\": \"/assets/images/322/image.jpg\"}]",
  }
]

当我尝试循环上述集合并抛出错误时: TypeError: lists.imageData.map is not a function ```
this.productList.map((lists) => {
lists.imageData.map((data) => {
data.imageId
})
})

所以我想要呈现的结果如下,这样我就可以在记录中循环。

[ RowDataPacket {
productId: 3,
imageData [
{
imageId: 18,
imageUrl: /assets/images/321/image.jpg
},
{
imageId: 17,
imageUrl: /assets/images/322/image.jpg
}
]
}
]

在管理员工具中运行sql查询

SELECT
products.id AS productId,
GROUP_CONCAT(JSON_ARRAY(JSON_OBJECT(
"imageId", product_images_ids.id,
"imageUrl", product_images.thumbnail_image
))) imageData
FROM products
JOIN product_images_ids ON product_images_ids.campaign_id = products.id
JOIN product_images ON product_images.id = product_images_ids.product_image_id
GROUP BY products.id

这是一个结果截图,我在管理员。
我已经检查了json_arrayagg和json_objectagg,但无法创建json对象数组。
p、 s:mysql版本:5.5.5-10.3.22-mariadb
yks3o0rb

yks3o0rb1#

放一个 CONCAT 内部 GROUP_CONCAT 创造 JSON 对象在这里
我不确定npm mariadb 模块将找出结构

let query = `SELECT 
  ${productTbl}.id AS productId,
  GROUP_CONCAT(
    CONCAT('{imageId:"'${prodImgTbl}'", imageUrl:"'${productImg}.thumbnail_image'"}')) imageData
  FROM ${productTbl} 

  JOIN ${prodImgTbl} ON ${prodImgTbl}.product_id = ${productTbl}.id
  JOIN ${productImg} ON ${productImg}.id = ${prodImgTbl}.product_image_id
  GROUP BY ${productTbl}.id`

如果不起作用,请尝试将其转换为json类型(整个组或其中的每个表达式)

CAST(
  CONCAT(
    '[', GROUP_CONCAT(
      CONCAT('{"imageId": "'${prodImgTbl}'", 
        "imageUrl": "'${productImg}.thumbnail_image'"}')),']') 
  AS JSON) imageData

如果您使用的是最新的mariadb版本,那么就有json\u对象

GROUP_CONCAT(JSON_OBJECT("imageId", ${prodImgTbl}, "imageUrl", ${productImg}.thumbnail_image)) imageData

---编辑
你可以打电话 JSON.parse 在每个 imageData ```
this.productList = map(pr => ({...pr, imageData: JSON.parse(pr.imageData)}))

显然之前也有类似的问题,这里他们建议在查询中创建整个json。

`SELECT JSON_OBJECT(
'productId',
${productTbl}.id,
'imageData',
CAST(
CONCAT(
'[', GROUP_CONCAT(
CONCAT('{"imageId": "'${prodImgTbl}'",
"imageUrl": "'${productImg}.thumbnail_image'"}')),']')
AS JSON)
)
FROM ${productTbl}

JOIN ${prodImgTbl} ON ${prodImgTbl}.product_id = ${productTbl}.id
JOIN ${productImg} ON ${productImg}.id = ${prodImgTbl}.product_image_id
GROUP BY ${productTbl}.id`

不幸的是你还得打电话 `JSON.parse` ```
let records = JSON.parse(await pool.query(query));

还有一个json\u objectagg

let query = `SELECT 
  ${productTbl}.id AS productId,
  JSON_OBJECTAGG(${prodImgTbl}, ${productImg}.thumbnail_image) imageData
  FROM ${productTbl} 

  JOIN ${prodImgTbl} ON ${prodImgTbl}.product_id = ${productTbl}.id
  JOIN ${productImg} ON ${productImg}.id = ${prodImgTbl}.product_image_id
  GROUP BY ${productTbl}.id`

但我认为您仍然需要进行一些连接,才能将上面的内容作为数组和调用 JSON.parse 不管怎样。

相关问题