php - 带有while循环的mysql Node js动态数组

标签 php mysql node.js express

我在 php 中有以下代码,效果很好

if ($result = $mysqli->query("SELECT t.*, GROUP_CONCAT(c.category) categories, GROUP_CONCAT(k.keyword) keywords FROM dataclayTemplates t LEFT JOIN dataclayCategoryLink cl JOIN dataclayCategories c ON cl.categoryId=c.id ON t.id=cl.templateId LEFT JOIN dataclayKeywordLink kl JOIN dataclayKeywords k ON kl.keywordId=k.id ON t.id=kl.templateId GROUP BY t.id"))
{
  while($row = $result->fetch_array(MYSQLI_ASSOC)) {
    if($row["categories"] == null) {
      $row["categoryArray"] = [];
    } else {
      $row["categoryArray"] = array_unique(explode(",",$row["categories"]));
    }
    unset($row["categories"]);
    if($row["keywords"] == null) {
      $row["keywordArray"] = [];
    } else {
      $row["keywordArray"] = array_unique(explode(",",$row["keywords"]));
    }
    unset($row["keywords"]);
    $templateArray[] = $row;
  }
}

$result->close();

我想在 nodejs 中使用 mysql 做同样的事情(在每个结果的末尾添加一个 categoryArray 数组,并用每个结果类别中的类别填充它) 我有这个 Node js 代码并且可以很好地提取数据我只需要构建 categoryArray 和 keywordArray。

import express from 'express';
import connection from '../index.js'
const router = express.Router();
router.get('/allTemplates', function (req, res) {

    let queryString="SELECT t.*, GROUP_CONCAT(c.category) categories, GROUP_CONCAT(k.keyword) keywords FROM dataclayTemplates t LEFT JOIN dataclayCategoryLink cl JOIN dataclayCategories c ON cl.categoryId=c.id ON t.id=cl.templateId LEFT JOIN dataclayKeywordLink kl JOIN dataclayKeywords k ON kl.keywordId=k.id ON t.id=kl.templateId GROUP BY t.id";
    let query = connection.query(queryString, (error, result) => {
        if(error) {throw error;}
        res.json(result);
    })
});
module.exports = router;

下面是我尝试在 nodejs 中执行此操作,但它不起作用。

import express from 'express';
import connection from '../index.js'
const router = express.Router();
router.get('/allTemplates', function (req, res) {

    let queryString="SELECT t.*, GROUP_CONCAT(c.category) categories, GROUP_CONCAT(k.keyword) keywords FROM dataclayTemplates t LEFT JOIN dataclayCategoryLink cl JOIN dataclayCategories c ON cl.categoryId=c.id ON t.id=cl.templateId LEFT JOIN dataclayKeywordLink kl JOIN dataclayKeywords k ON kl.keywordId=k.id ON t.id=kl.templateId GROUP BY t.id";
    let query = connection.query(queryString, (error, result) => {
        if(error) {throw error;}
        let categoryArray=[];
        result.forEach(function(template){ 
        template.concat(categoryArray);
    template.categories.forEach(function(category) { 
        if(template.categories!=null){
      template.categoryArray.push(category);
        }
    });
});
    })
});
module.exports = router;

如果有任何帮助,我们将不胜感激。

最佳答案

我不得不稍微修改我的代码,但这就是我让它工作的方式。希望它能帮助别人。编码愉快!

router.get("/", async (req, res, next) => {
  const data = {};
  const sqlStatement =
    "SELECT t.*, GROUP_CONCAT(DISTINCT c.category) categories, GROUP_CONCAT(DISTINCT k.keyword) keywords FROM dataclayTemplates t LEFT JOIN dataclayCategoryLink cl JOIN dataclayCategories c ON cl.categoryId=c.id ON t.id=cl.templateId LEFT JOIN dataclayKeywordLink kl JOIN dataclayKeywords k ON kl.keywordId=k.id ON t.id=kl.templateId GROUP BY t.id;";

  try {
    data.categories = await database.query("SELECT * FROM dataclayCategories");
    data.keywords = await database.query("SELECT * FROM dataclayKeywords");
    data.templates = await database.query(sqlStatement);
    for (let i = 0; i < data.templates.length; i++) {
      const current = data.templates[i];
      if (current.categories) {
        current.categories = current.categories.split(",");
      }
      if (current.keywords) {
        current.keywords = current.keywords.split(",");
      }
    }

    res.json(data);
  } catch (error) {
    next(error);
  }
});

关于php - 带有while循环的mysql Node js动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53106877/

相关文章:

javascript - node.js 识别字符模式但不识别数字模式

Node.js,Angular,快速 session : Chrome 80 does not save session because of cookie policy (sameSite cookies)

php - 用户喜欢/收藏的 Youtube API 调用

php - reCAPTCHA PHP 和 AJAX

PHP 从多维数组创建多个表

mysql - 为什么 node.js 的 Mysql native 驱动程序的查询执行时间如此之高?还有其他选择吗?

mysql - 嵌套查询中的 where in 子句

php - WordPress 联系表 7 : can I send 2 different emails?

mysql如何在where子句中使用相同的子查询更新表

node.js - 与kong集成的微服务的用户注册+认证