データをJsonで返したいですが、方法がわかりません

前提

idSong関数のところなんですが、指定したidのname とratingをJsonで返したいです。
データベースのテーブルは、以下のようになっています。
CREATE TABLE songs (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
rating INTEGER NOT NULL
);

実現したいこと

例えば、id1を指定した場合、{"id":1,"name":"song-name","rating":5}と返るようにしたいです。

発生している問題・エラーメッセージ

今は、このように{ }返ってきます。

該当のソースコード

import { Application, Router } from "https://deno.land/x/oak@v10.6.0/mod.ts"; import { executeQuery } from "./database/database.js"; const app = new Application(); const router = new Router(); const listSongs = async ({ response }) => { const result = await executeQuery("SELECT id, name, rating FROM songs;"); response.body = result.rows; }; const addSong = async ({ request, response }) => { const body = request.body({ type: "json" }); const document = await body.value; await executeQuery("INSERT INTO songs (song, rating) VALUES ($name, $rating);", { name: document.name, rating: document.rating, }); response.body = { status: "success" }; }; //下記が質問のところです。 const idSong = async({ params,response }) => { const result = await executeQuery("SELECT FROM songs WHERE id = $id;", { id: params.id },); response.body = result.rows[0]; }; const deleteSong = async({ params, response }) => { await executeQuery("DELETE FROM songs WHERE id = $id;", { id: params.id },); response.body = { status: "success" }; }; router.get("/songs", listSongs); router.get("/songs/:id", idSong); router.post("/songs", addSong); router.post("/songs/:id", deleteSong); app.use(router.routes()); app.listen({ port: 7777 });

試したこと

JSON.parse(result.rows[0])としてみましたが、Nullが返ってきました。

補足情報(FW/ツールのバージョンなど)

コメントを投稿

0 コメント