Skip to content

Commit 2cae923

Browse files
committed
fix creating todo endpoint
1 parent bc8fb64 commit 2cae923

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

client/src/demo/Demo.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ const Demo = () => {
7676
data: { todoContent: newTodo }
7777
});
7878
if (res.status === 201) {
79-
await getTodos();
79+
setTodos((prevState) => [res.data, ...prevState]);
8080
}
8181
} catch (err) {
82-
console.error(err.response.data);
82+
console.error(err.response?.data);
8383
} finally {
8484
setNewTodo('');
8585
}

server/src/api/routes/routes.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ router.post('/create_todo', async (req, res) => {
1919
const { todoContent } = req.body;
2020
try {
2121
await db.query(`INSERT INTO Todolist(todo) VALUES ($1)`, [todoContent]);
22-
res.status(201).send();
22+
const sqlRes = await db.query(
23+
`SELECT * FROM Todolist ORDER BY created_at DESC limit 1`
24+
);
25+
res.status(201).send(sqlRes.rows[0]);
2326
} catch (err) {
2427
res.status(500).send(`There was an error while creating ${todoContent}`);
2528
}

0 commit comments

Comments
 (0)