티스토리 뷰

728x90
반응형

배송비 라는 한글을 url에서는 깨진다. 심지어  스페이스바 한번쳐도 url에서는 다르게 나타난다.

즉, 그러므로 한글을 입력받을 때 url이 깨지지 않도록 해야한다.

 

controllers.ts

/**
   * @url [GET] /api/coupons/owned-coupons
   * @description 사용자 보유쿠폰 조회
   *              쿠폰타입에 해당하는 쿠폰들 검색
   *              유효기간이 만료되지 않고, 사용 가능한 쿠폰들만 존재합니다.
   * @Request
   * @Response OwnedCoupons[]
   * @success 200
   * @errorCode 400
   */
  @UseGuards(AuthenticatedGuard)
  @Get('owned-coupons')
  async getUserOwnedCoupon(@User() user: Users, @Query('couponTypes') couponTypes?: string) {
    return await this.couponsService.getUserOwnedCoupons(user, couponTypes);
  }

쿼리파라미터에 한글데이터를 넣었는데 검색에러가 발생했다.

ERROR [ExceptionsHandler] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''배송비'' at line 1

 

[Nest] 33507  - 2022. 11. 04. 오전 2:45:43   ERROR [ExceptionsHandler] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''배송비'' at line 1
QueryFailedError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''배송비'' at line 1
    at Query.onResult (/Users/ek/Documents/github/loveAlakazam/2_shopping_mall_management/src/driver/mysql/MysqlQueryRunner.ts:222:33)
    at Query.execute (/Users/ek/Documents/github/loveAlakazam/2_shopping_mall_management/node_modules/mysql2/lib/commands/command.js:36:14)
    at PoolConnection.handlePacket (/Users/ek/Documents/github/loveAlakazam/2_shopping_mall_management/node_modules/mysql2/lib/connection.js:456:32)
    at PacketParser.onPacket (/Users/ek/Documents/github/loveAlakazam/2_shopping_mall_management/node_modules/mysql2/lib/connection.js:85:12)
    at PacketParser.executeStart (/Users/ek/Documents/github/loveAlakazam/2_shopping_mall_management/node_modules/mysql2/lib/packet_parser.js:75:16)
    at Socket.<anonymous> (/Users/ek/Documents/github/loveAlakazam/2_shopping_mall_management/node_modules/mysql2/lib/connection.js:92:25)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Socket.Readable.push (node:internal/streams/readable:234:10)

 

[해결과정]

원인은 인코딩이 euc-kr 이고, Node.js는 utf-8을 기본으로 받아들인다.

"%EB%B0%B0%EC%86%A1%EB%B9%84,%EC%A0%95%EC%95%A1%EC%A0%9C,%20%ED%8D%BC%EC%84%BC%ED%8A%B8%20%ED%95%A0%EC%9D%B8" 로 인코딩 된 값을

"배송비,정액제,   퍼센트 할인" 로 decode 하고싶다.

그러나 해봤지만,, 여전하다 ㅠㅠ

 

encoded = encodeURI(한글) 

-> encoded에는 깨진글이 나온다. 

 

decodeURI (encoded) 

-> 다시 한글로 나온다.

 

이건 필터링 검색이다. couponTypes 가 "배송비" 와 같이 한 개 종류만 검색하는 게 아니라

couponTypes 가 [ "배송비", "퍼센트 할인"] 일때 2개이상의 종류도 검색하는 것을 목표로 했다!

 

인코드, 디코드의 문제가 아니었다.

서비스 로직에서 In구문에서 잘못됐다!

원인: 

sql.andWhere('coupons.couponType IN :couponTypes', { couponTypes: couponTypeList });

 

해결:

sql.andWhere('coupons.couponType IN (:couponTypes)', { couponTypes: couponTypeList });

:couponTypes 는 string[] 타입이다. 

IN ( 배열 ) 형태임을 잊지말자!

 

 

https://doug-martin.github.io/nestjs-query/docs/concepts/queries/

 

Queries | Nestjs-query

The core of nestjs-query is the Query, it is used by @nestjs-query/query-graphql, @nestjs-query/query-typeorm

doug-martin.github.io


[참고]

 

파라미터로 받은 url의 한글 깨짐 현상 방지

http://tingo.test/searchlist/?q=짱구 라는 url을 이용해서 검색결과를 바로 보여주고 검색어를 상단에 노출시키려고 하는데그냥 검색어 텍스트를 변경없이 가져오게 되면 검색은 정상적으로 되지만 짱

velog.io

 

 

javascript querystring 주고 받을 때 한글 깨짐 처리 방법 / 쿼리스트링 파싱

와나 이거 때문에 개고생했다. 어제 점심 먹고부터 구글링해서 오늘 점심에 밥먹고 엎드려 잘때 꿈까지 꾼 ...

blog.naver.com

 

Node.js 에서 request 한글 깨짐 문제

Node.js 에서 request 모듈을 사용하여 학교 홈페이지를 크롤링 하는 도중 한글이 깨지는 문제가 발생하였다.

lunatk.github.io

 

 

Redirect 시 한글 파라미터가 깨지는 문제

이번에 토이 프로젝트를 하면서 발견한 문제인데Spring controller에서 redirect:로 redirection 응답 시 파라미터 값에 한글이 들어가 있을 경우 ?? 로 파라미터 값이 전달되는 문제를 발견했다. 간단하지

ivvve.github.io

 

728x90
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함