티스토리 뷰
배송비 라는 한글을 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/
[참고]
'Backend > 꾸준히 TIL' 카테고리의 다른 글
[NestJS + TypeORM] 소수넘버 필드, 길이 설정 (0) | 2022.11.05 |
---|---|
[에러 해결] Cannot find module bcrypt_lib.node (0) | 2022.11.05 |
[nestJs + typeorm] 날짜데이터를 insert 하기 (0) | 2022.11.03 |
Type number trivaially inferred from a number literal, remove type annotation 에러 해결 (0) | 2022.11.03 |
package-lock.json 잦은 변경을 막고 싶어요. (0) | 2022.10.29 |
- Total
- Today
- Yesterday
- MongoDB
- vscode
- git
- 참고
- nestjs
- Jekyll
- 한달어스
- 미완
- 개발용어
- 한달독서
- typeORM
- TypeScript
- TDD
- jest
- 바이트디그리
- node.js
- Nest.js
- 갓생살자
- 나도 할 수 있다
- Mongoose
- gem
- 습관개선
- 클린아키텍쳐
- MySQL
- OS
- IT용어
- 스마트폰중독
- nestjs jest
- RDBMS
- 디지털디톡스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |