1

I am following RESTful API series of Academind and I have added user signup but I am getting error of Cannot read property 'email' of undefined upon passing following json data by postman in raw format of json, everything of postman is configured as in the academind video.

{
    "email" : "test@test.com",
    "password" : "tester"
}

My routes/users.js look like this:

const express = require('express');
const router = express.Router();
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");

const User = require("../models/user");

router.post('/signup', (res, req, next) => { 
    // console.log(req.body.email);
    User.find({ email: req.body.email })
    .exec()
    .then(user => {
        if ( user.length >= 1 ) {
            return res.status(409).json({
                message: 'Email already in use by some other user'
            });
        } else {
            bcrypt.hash(req.body.password, 10, (err, hash) => {
                if (err) {
                    return res.status(500).json({
                        error: err,
                    });
                } else {
                    // console.log(req.body.email);
                    const user = new User({
                        _id: new mongoose.Types.ObjectId(),
                        email: req.body.email,
                        password: hash,
                    });
                    user
                    .save()
                    .then(result => {
                        console.log(result);
                        res.status(201).json({
                            message: "User Created Successfully",
                        });
                    })
                    .catch(err => {
                        console.log(err);
                        res.status(500).json({
                            error: err
                        });
                    });
                }
            });
        }
    });
});

module.exports = router;
Ali Azlan
  • 77
  • 1
  • 9
  • 2
    Please step through this under a debugger (Chrome Developer Tools, for example) and find out which object you *EXPECT* to have an "email" property is instead *UNDEFINED* (or "null"). I'm guessing the problem might be that you're not getting `req.body`. Please update your post. – paulsm4 Jun 28 '20 at 14:39
  • Why, under the commented console log, there is req.user.email? Shouldn't it be req.body.email? – Ambu Jun 28 '20 at 14:43
  • @paulsm4 sorry bro i was using req.body i just used req.user for one time to find out if this was the problem but then forgot to change it back to req.body again and i posted it like i was using req.user which is not true. Simply saying req.body is not resolving error. I edit my question to change req.user to req.body – Ali Azlan Jun 28 '20 at 15:10
  • @Ambu where? no where under commented console.log there is req.user, i haven't edit it also – Ali Azlan Jun 28 '20 at 15:14
  • I meant the one in your first comment. It's gone now. – Ambu Jun 28 '20 at 15:55
  • 2
    Q: Are you using a debugger? The comments about "console.log" (which I'm *NOT* seeing in your posts: pardon me if I missed it), imply "Yes". Debuggers are Good. Please confirm you're using one. Q: In the debugger - *CAN YOU IDENTIFY WHICH OBJECT IS "UNDEFINED"???* That's your goal. If you identify the "undefined object" - you solve the problem :) – paulsm4 Jun 28 '20 at 16:26
  • 2
    Hi, cool that you figured it out. But it would be best to remove the 2nd edit with the actual solution from the question and instead add an _answer_ with the solution. You can then later even accept your own answer. This way it's clear for others what the problem and solution were. – CherryDT Jun 28 '20 at 16:31
  • @paulsm4 I m not using any debugger. As i have mentioned in my second edit that I compared my code with the actual code on github which lead me to find the root cause of the error. – Ali Azlan Jul 02 '20 at 10:41
  • @CherryDT idk what u talking abt because my 2nd edit tells the solution so i don't know why should i remove it ,to again edit it to tell the solution – Ali Azlan Jul 02 '20 at 10:42
  • No, I meant to add an actual _answer_ instead of editing your question. Then you can accept your own answer and the question is shown as solved. – CherryDT Jul 02 '20 at 11:16
  • https://i.stack.imgur.com/r2P7U.png – CherryDT Jul 02 '20 at 11:17
  • @CherryDT thank u bro. I just removed all unnecessary edits and other code from the question and have answered my question as u told me to. – Ali Azlan Jul 03 '20 at 17:57
  • By the way: "I'm not using any debugger" << you should change that. It's tremendously useful. I know it sounds foreign and complicated at first but it is most direct way to understand what your code actually does and it's not as complicated as it may first appear. I would highly recommend learning how to use one. You can even use the devtools in Chrome for that - run your app with the `--inspect` node argument, e.g. `node --inspect server.js`, and then open `chrome://inspect` in your browser and select the node.js devtools. It will automatically attach to your application. – CherryDT Jul 03 '20 at 18:38
  • @CherryDT ok i will learn... by the way if are familiar with googleoauth2.0 then help me with my new problem please. here is the link to that https://stackoverflow.com/questions/62720465/invalid-parameter-value-for-redirect-uri-non-public-domains-not-allowed-http – Ali Azlan Jul 04 '20 at 09:12

1 Answers1

1

On this line:

router.post('/signup', (res, req, next) => { 

the arguments inside the arrow function should be like this (req, res, next)=> that is "req" comes before "res" in arguments. like this: router.post('/signup', (req, res, next) => {

Ali Azlan
  • 77
  • 1
  • 9