From 3a44159a2aa378d2ec9108b0ac6874d87a62f879 Mon Sep 17 00:00:00 2001 From: xuvi7 <07xuvi@gmail.com> Date: Thu, 21 Sep 2023 23:19:31 -0400 Subject: [PATCH] Fix duplicate users in database --- server/index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/server/index.js b/server/index.js index e575a38..acc4b2a 100644 --- a/server/index.js +++ b/server/index.js @@ -58,11 +58,18 @@ app.post('/api/login', async (req, res) => { app.post('/api/create', async (req, res) => { console.log(req.body) try { - const journal = await Journal.create({ - email: req.body.email, - content: req.body.content, + const exist = await Journal.findOne({ + email: req.body.email }) - res.json({ status: 'ok' }) + if (!exist) { + const journal = await Journal.create({ + email: req.body.email, + content: req.body.content, + }) + res.json({ status: 'ok' }) + } else { + res.json({ status: 'error', error: 'email already exists' }) + } } catch (err) { console.log(err) res.json({ status: 'error', error: 'error' })