db.query(`SELECT * FROM topic LEFT JOIN author ON topic.author_id=author.id WHERE topic.id=?` ,[queryData.id], function(error2, topic, fields){
if(error2){
throw error2; //error2를 출력하면서 에러를 던짐
}
console.log(topic)
...
}
이렇게 데이터를 가져왔다.
그런데 문제가 발생했다.
author 테이블
topic 테이블
두 테이블에 같은 이름을 가진 컬럼 id
가 있기 때문이다.
위와 같은 방식으로 가져왔을 때 topic
은 id 값으로 author 테이블의 id값을 id변수로 가져왔다.
db.query(`SELECT *, topic.id as id FROM topic LEFT JOIN author ON topic.author_id=author.id WHERE topic.id=?`,[queryData.id], function(error2, topic, fields){
`SELECT *, topic.id as id FROM topic
위의 쿼리문과 같이 as를 사용해 id 값으로 가져오고싶은 데이터를 명시해주는 것으로 해결할 수 있었다.