password: ValidatorError: Path `password` is required. Node JS, express

Issue

I’m trying to make authorization with refresh tokens and access tokens via Express.
So I’ve met an issue that path password value after postman call is undefined,
in spite of it logs in console good.

user controller:

class UserController {
  async registration(req, res, next) {
    try {
      const { email, password } = req.body;
      console.log("pass from user controller " + password);
      const userData = await userService.registration(email, password);
      res.cookie("refreshtoken", userData.refreshToken, {
        maxAge: 30 * 24 * 60 * 60 * 1000,
        httpOnly: true,
      });
      return res.json(userData);
    } catch (error) {
      console.log(error);
    }
  }
}

user model:

import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({
  email: {
    type: String,
    unique: true,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  isActivated: { type: Boolean, default: false },
  activationLink: { type: String },
});
export default mongoose.model("User", UserSchema);

user service:

import UserModel from "../models/user-model.js";
import bcrypt from "bcrypt";
import { v4 as uuid } from "uuid";
import mailService from "./mail-service.js";
import tokenService from "./token-service.js";
import UserDto from "../dtos/user-dto.js";
class UserService {
  async registration(email, password) {
    const candidate = await UserModel.findOne({ email });
    console.log("pass form user Service " + password);
    if (candidate) {
      throw new Error(`Usser with this email ${email} already exists`);
    }
    const hashPassword = await bcrypt.hash(password, 3);
    const activationLink = uuid();
    const user = await UserModel.create({
      email,
      hashPassword,
      activationLink,
    });
    await mailService.sendActicvationMail(email, activationLink);
    const userDto = new UserDto(user);
    const tokens = tokenService.generateToken({ ...userDto });
    await tokenService.saveToken(userDto.id, tokens.refreshToken);
    return {
      ...tokens,
      user: userDto,
    };
  }
}
export default new UserService();

I’m doing it with guide, so maybe I’ve made some silly mistake that I ain’t see.

Full error:

errors: {
password: ValidatorError: Path password is required.
at validate (/home/galich/Desktop/backend/jwt-authorization/server/node_modules/mongoose/lib/schematype.js:1337:13)
at SchemaString.SchemaType.doValidate (/home/galich/Desktop/backend/jwt-authorization/server/node_modules/mongoose/lib/schematype.js:1321:7)
at /home/galich/Desktop/backend/jwt-authorization/server/node_modules/mongoose/lib/document.js:2831:18
at processTicksAndRejections (node:internal/process/task_queues:78:11) {
properties: [Object],
kind: ‘required’,
path: ‘password’,
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
}
},
_message: ‘User validation failed’
}

Solution

You need to modify this snippet

const user = await UserModel.create({
      email,
      hashPassword,
      activationLink,
    });

to

const user = await UserModel.create({
  email,
  password: hashPassword,
  activationLink,
});

Answered By – Andrew Wang

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published