Skip to content

Commit 17d2579

Browse files
committed
fix(js): handle exception
1 parent 7a45a6e commit 17d2579

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

sqlite-cloud/quick-start-node.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ const connectionString = process.env.SQLITECLOUD_CONNECTION_STRING
3636
const app = express();
3737

3838
app.get("/albums", async (req, res) => {
39+
const db;
3940
try {
40-
const db = new Database(connectionString)
41+
db = new Database(connectionString)
4142
const result = await db.sql(`
4243
USE DATABASE chinook.sqlite;
4344
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
@@ -48,6 +49,8 @@ app.get("/albums", async (req, res) => {
4849
res.json(result);
4950
} catch (error) {
5051
res.status(500).json({ error: error.message });
52+
} finally {
53+
db?.close();
5154
}
5255
});
5356

sqlite-cloud/quick-start-react-native.mdx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,24 @@ export default function App() {
4646

4747
useEffect(() => {
4848
async function getAlbums() {
49-
const db = new Database('<your-connection-string>');
50-
51-
const result =
52-
await db.sql(`USE DATABASE chinook.sqlite;
53-
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
54-
FROM albums
55-
INNER JOIN artists
56-
WHERE artists.ArtistId = albums.ArtistId LIMIT 20;`);
57-
58-
setAlbums(result);
49+
const db;
50+
try {
51+
db = new Database('<your-connection-string>');
52+
53+
const result =
54+
await db.sql(`USE DATABASE chinook.sqlite;
55+
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
56+
FROM albums
57+
INNER JOIN artists
58+
WHERE artists.ArtistId = albums.ArtistId LIMIT 20;`);
59+
60+
setAlbums(result);
61+
}
62+
} catch (error) {
63+
// manage error state
64+
console.error(`getAlbums - ${error}`, error)
65+
} finally {
66+
db?.close();
5967
}
6068

6169
getAlbums();

sqlite-cloud/quick-start-react.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ function App() {
3535
const [data, setData] = useState([]);
3636

3737
const getAlbums = async () => {
38+
const db;
3839
try {
39-
const db = new Database(connectionString)
40+
db = new Database(connectionString)
4041
const result = await db.sql(`
4142
USE DATABASE chinook.sqlite;
4243
SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist
@@ -46,7 +47,10 @@ function App() {
4647
LIMIT 20;`);
4748
setData(result);
4849
} catch (err) {
49-
// manage error case
50+
// manage error state
51+
console.error(`getAlbums - ${error}`, error);
52+
} finally {
53+
db?.close();
5054
}
5155
};
5256

0 commit comments

Comments
 (0)