Skip to content

POST API "Cannot read properties of undefined (reading 'findOne')" #1

@HSinghHira

Description

@HSinghHira

Hi there,

Thanks so much for sharing your code!

I'm currently on Day 7, and I ran into an error while using your code in authController.js for a POST API request.

The error message was:

{
  "success": false,
  "message": "Cannot read properties of undefined (reading 'findOne')"
}

I was able to fix the issue by making some changes:

  try {
    let user = await mongoose.connection
      .collection("users")
      .findOne({ username });
    if (user) {
      return res
        .status(400)
        .json({ success: false, message: "Username already in use" });
    }
    const securePassword = await bcrypt.hash(password, 10);

    const userModel = mongoose.model("User");
    user = new userModel({
      username,
      email,
      password: securePassword,
      accountType,
    });
    await user.save();

Could you please take a look and see if these changes are safe and follow best practices?

Really appreciate your work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions