Fix type errors
This commit is contained in:
parent
bd9f8c9a60
commit
5587b88cf1
32
lib/books.ts
32
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<DetailedBook> {
|
||||
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<DetailedBook> = 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 {
|
||||
|
@ -1,8 +1,19 @@
|
||||
import type { NextPage } from "next";
|
||||
import React from "react";
|
||||
|
||||
const Home: NextPage = () => {
|
||||
return <main>I am a book</main>;
|
||||
};
|
||||
import { getBook, DetailedBook } from "../lib/books";
|
||||
|
||||
export default Home;
|
||||
export default function Home(props: Props) {
|
||||
return <p>{props.book.title}</p>;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
book: DetailedBook;
|
||||
}
|
||||
|
||||
export async function getServerSideProps() {
|
||||
return {
|
||||
props: {
|
||||
book: await getBook(2),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user