728x90
SMALL
상황
images라는 table에 올라간 사진 여러장을 웹 페이지에 띄우기 위해
db.get("SELECT imageSrc FROM images WHERE owner=?",
라는 쿼리를 사용했으나, 사진 한장만 반환되었다.
db.get("SELECT imageSrc, count(*) FROM images WHERE owner=?",
이때 이렇게 count(*)를 이용하면 1이상의 수가 제대로 들어오는 것을 알 수 있다.
해결
라우팅하는 js파일에
db.all("SELECT imageSrc FROM images WHERE owner=?",
all method를 사용하고,
render되는 html에
<% srcRow.forEach(row => { %>
<% var tmp = JSON.stringify(row.imageSrc) %>
<% console.log(tmp) %>
<img src ="<%=tmp.substring(1, tmp.length-1)%>">
<% }) %>
을 하면
이렇게 여러 행이 console에 찍히는 것을 볼 수 있다.
그러나 all method는
데이터베이스의 모든 항목을 메모리로 반환한 다음 쿼리를 실행하고 다음에서 콜백을 호출하기 때문에,
데이터베이스에서 일부 항목을 검색할 때는 each method를 사용하는 것이 좋다.
다음 글엔 each method를 사용해 코드를 수정하는 과정을 기록하겠다.
참고
https://www.sqlitetutorial.net/sqlite-nodejs/query/
https://discuss.codecademy.com/t/why-use-db-each-instead-db-all-or-db-get-in-node-sqlite/381382
728x90
LIST
'Node.js' 카테고리의 다른 글
User.findOne is not a function (0) | 2022.03.13 |
---|---|
[출석부 만들기] mongoose 연결하고 mongoDB compass에서 확인하기 (0) | 2022.02.21 |
백, 프론트 서버 동시 실행 시 한쪽을 무시해요 (0) | 2022.02.15 |
[MongoDB] 몽구스 스키마 설계, password 암호화, 저장 (0) | 2022.02.13 |
TypeError: "x" is not a constructor (0) | 2022.02.13 |
Comment