【node-express】实现省县市/区三级联动接口

省县市/区三级联动接口

介绍

源码地址:https://github.com/thinkasany/nestjs-course-code/tree/master/demo/address
使用 navicat 导入sql文件,新增表,然后只需要一个接口 localhost:3001/region?parentId=1, 不断的根据id去查询后续的数据。
在这里插入图片描述

接口步骤

  1. 首先默认使用parentId返回所有省的字段
    在这里插入图片描述
  2. 再根据省的id,比如浙江12
    在这里插入图片描述
  3. 再根据市的id返回区

代码部分

const { ShopRegion } = require('../model/mysql');
const getRegion = async(req, res) => {
  const parent_id = Number(req.query.parentId);
  try {
    const regions = await ShopRegion.findAll({ where: {parent_id} });
    return res.json({ message: 'OK', data: regions });
  } catch (error) {
    console.error('Error fetching region data:', error);
    res.status(500).json({ error: 'Internal Server Error' });
  }
};

module.exports = {
  getRegion
};

相关推荐

  1. mysql全国三级联动创表sql(一)

    2023-12-28 22:48:01       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 22:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 22:48:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 22:48:01       82 阅读
  4. Python语言-面向对象

    2023-12-28 22:48:01       91 阅读

热门阅读

  1. 视频人脸识别马赛克处理

    2023-12-28 22:48:01       97 阅读
  2. 20231228 SQL基础50题打卡

    2023-12-28 22:48:01       66 阅读
  3. SQL进阶:Case语句使用

    2023-12-28 22:48:01       54 阅读
  4. PostgreSql 索引使用技巧

    2023-12-28 22:48:01       58 阅读
  5. 音视频编码基础知识

    2023-12-28 22:48:01       66 阅读
  6. 【DB2】Maxlocks和防止锁升级

    2023-12-28 22:48:01       57 阅读
  7. 力扣labuladong——一刷day80

    2023-12-28 22:48:01       57 阅读
  8. 面试问题整理若干

    2023-12-28 22:48:01       61 阅读
  9. 观察者(模板)的一点体会

    2023-12-28 22:48:01       50 阅读
  10. 大数据知识分享:大数据产业必知概念

    2023-12-28 22:48:01       63 阅读
  11. SQL备忘--子查询与ALL/ANY运算符

    2023-12-28 22:48:01       57 阅读
  12. 幸运树。。

    2023-12-28 22:48:01       53 阅读
  13. Oracle中varchar2和nvarchar2的区别

    2023-12-28 22:48:01       57 阅读
  14. Qt中保存和还原Widget状态的入门指南

    2023-12-28 22:48:01       68 阅读