From 5587b88cf17b26f70b59cdf1bd106a2832453be5 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Tue, 15 Feb 2022 22:24:26 -0500 Subject: [PATCH] Fix type errors --- lib/books.ts | 32 ++++++++++++++++++++++++++------ pages/index.tsx | 21 ++++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/books.ts b/lib/books.ts index 3144413..252ac27 100644 --- a/lib/books.ts +++ b/lib/books.ts @@ -2,19 +2,39 @@ import sqlite3 from "sqlite3"; const DATABASE_PATH = "catalogue.db"; -async function getBook(id: number) { +export async function getBook(id: number): Promise { const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); + const sql = "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated, deleted FROM books WHERE id = ? "; - await database.get(sql, [id], (err: Error | null, book: DetailedBook) => { - if (err) { - return console.error(err.message); - } - console.log(book); + const book: Promise = new Promise((resolve, reject) => { + database.get(sql, [id], (err: Error | null, book: DetailedBook) => { + if (err) { + reject(err); + } + if (book) { + resolve(book); + } else { + reject(new Error("Not a valid id")); + } + }); }); + // .then((newBook) => { + // book = new Promise((resolve) => { + // resolve(newBook); + // }); + // }) + // .catch((err) => { + // console.error(err); + // }) + // .finally(() => { + // database.close(); + // }); database.close(); + + return book; } export interface DetailedBook { diff --git a/pages/index.tsx b/pages/index.tsx index cf73651..df94b40 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,8 +1,19 @@ -import type { NextPage } from "next"; import React from "react"; -const Home: NextPage = () => { - return
I am a book
; -}; +import { getBook, DetailedBook } from "../lib/books"; -export default Home; +export default function Home(props: Props) { + return

{props.book.title}

; +} + +interface Props { + book: DetailedBook; +} + +export async function getServerSideProps() { + return { + props: { + book: await getBook(2), + }, + }; +}