2024. 2. 26. 23:57ㆍ공부/내배캠 TIL
목차
1. 오류 내용
1. 첫번째 에러
SyntaxError: Expected property name or '}' in JSON at position 3
~~~~~~~
간단한 문법 오류.
{
'email':'kimgrang1202@gmail.com'
}
json 파일을 이렇게 작성하니 문법 오류가 뜰 수 밖에.
{
"email":"kimgrang1202@gmail.com"
}
2. 두번째 문제.
if (decodedToken.email === email) {
console.log('verify_email_3333')
const updatedVerify = await usersRepository.updateUserByEmail({
where: { email: email },
data: {
verify: true
}, data
});
console.log("코드 끝", updatedVerify)
if (decodedToken.email === email) {
const data = {
verify: true
}
const updatedVerify = await usersRepository.updateUserByEmail(email, data);
return res.status(200).json({
message: '이메일 인가 완료.',
user: updatedVerify,
});
updateUserByEmail을 보면 바로 무엇이 문제인지 알 수 있다.
updateUserByEmail = async (email, data) => {
await dataSource.getRepository('users').update({
email: email,
}, data)
}
인자를 두개 집어넣는 함수에 email만 줬고, data가 undefined로 남아 오류를 발생시켰다.
2. 내용 정리
1. typeORM
// index.js
const dataSource = new DataSource({
type: 'mysql',
host: process.env.ORM_HOST,
port: process.env.ORM_PORT,
username: process.env.ORM_USERNAME,
password: process.env.ORM_PASSWORD,
database: process.env.ORM_DATABASE,
//database 동기화 명령(npx prisma db push랑 같음, 매우 위험)
synchronize: false,
entities: [Coupons, Menu, Orders, Point, Restaurant, Review, Users],
migrations: ['src/typeorm/migrations/*.js'],
cli: {
entitiesDir: 'src/typeorm/entities',
migrationsDir: 'src/typeorm/migrations',
},
logging: true,
});
await dataSource.initialize();
ormconfigs.js를 대신할 수 있는 코드 내용.
synchronize:true일시, 엔티티를 직접 수정할 수 있음 -> 이는 실수로 인한 변경이 db에 반영되어 문제가 발생할 수 있음.
-> 그렇기 때문에 직접 migration할 당위성이 생김.
2. migration
코드 가장 밑의
await dataSource.initialize();
를 주석처리 한다. -> 추후에 실행할 migration:run이 db에 연결하게 될 때, 위의 코드가 살아있을 경우 충돌이 생긴다.
이미 디렉토리를 지정해 뒀기 때문에( migrationsDir: 'src/typeorm/migrations', )
typeorm migration:create ./src/typeorm/migrations/<name>
-> 이번 프로젝트의 경우 yarn typeorm migration:create ./src/typeorm/migrations/UserRefactoring
cli 명령어를 실행해준다. 실행시 migrationDir 내에 숫자~UserRefactoring.ts라는 파일이 생긴다( typeORM이 js에서도 사용할 수 있지만, typescript 와 깐부관계이기 때문이다. )
ts파일을 아래와 같이 js에 맞게 바꾸면.
export class UserRefactoring1708924100623 {
async up(queryRunner) {
// 4. 포인트의 기본값을 업데이트합니다.
await queryRunner.query(`
ALTER TABLE users
CHANGE point point BIGINT DEFAULT 0;
`);
}
}
(백틱 내부에는 db에 날릴 rawQuery를 작성한다. + up은 적용, down은 롤백이다.)
yarn typeorm migration:run --dataSource src/typeorm/index.js
위의 코드를 실행하여 migration을 실행한다.
3. 생각 정리
최선을 다해 짠 코드가 하나도 작동하지 않을 때,
그나마 익숙하게 사용할 수 있었던 prisma를 버리고 typeorm을 채용했을 때,
prisma코드를 typeorm으로 바꾸면서 마주한 수많은 오류들과 마주했을 때,
팀원들의 도움으로 겨우겨우 문제를 해결하고 잠에들 수 있었습니다.
'공부 > 내배캠 TIL' 카테고리의 다른 글
[Node.js_4기] Redis, 트러블 슈팅 (24/02/28) (0) | 2024.02.28 |
---|---|
[Node.js_4기] TIL : 트러블슈팅 (24/02/27) (1) | 2024.02.27 |
[Node.js_4기] TIL : Layered Architecture Pattern (yy/mm/dd) (0) | 2024.02.19 |
[Node.js_4기] Today_I_Learn : 객체지향 (24/02/16) (0) | 2024.02.16 |
[Node.js_4기] TIL : 뉴스피드 프로젝트 회고 (24/02/14) (1) | 2024.02.15 |