0

(node:1984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'email' of undefined at app.use (C:\Users\niko\Desktop\test\app.js:15:24) at Layer.handle [as handle_request] (C:\Users\niko\Desktop\test\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:317:13) at C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:275:10) at expressInit (C:\Users\niko\Desktop\test\node_modules\express\lib\middleware\init.js:40:5) at Layer.handle [as handle_request] (C:\Users\niko\Desktop\test\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:317:13) at C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:284:7 (node:1984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

const express = require('express');
const mongodb = require('mongodb');
const uri = 'mongodb:http://localhost:27017/moneymarket'

const MongoClient = require('mongodb').MongoClient();

const app = express();

mongodb.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }, () => {
    console.log("connected to DB");
});

app.use('/', async (req, res, next) => {
    const email = req.body.email;
    console.log(User)
    
   //const email = req.body.email;

   const emailExist = await User.findOne({email: req.body.email});
   if (emailExist) {
    return res.send("email already exists")
   } 
});


app.listen(3000, () => console.log("Server Runing on localhost:3000"));

1 Answers1

0

If you are sending the data as JSON you need to add the JSON body parser:

// enable json body parsing
app.use(express.json());
// if change this to actually a post/put, something that has a body
app.post('/', async (req, res, next) => 

Hopefully that helps!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91