Chris Rutherford
Chris Rutherford

Reputation: 1672

Mongoose Model.findOne not a function

Having an issue with a model. Trying to do a model.findOne(), but I keep getting the error

TypeError: User.findOne is not a function

I have the model setup like so:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema({
  firstName: String,
  lastName: String,
  emailAddress: {
    type: String,
    required: true,
    unique: true
  },
  userName: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  }
});

module.export = User = mongoose.model("User", UserSchema);

and I have it imported in the file that I want to find a user:

const { Strategy, ExtractJwt } = require("passport-jwt");
const log = require("./logger");
require('dotenv').config();
const fs = require("fs");
const secret = process.env.SECRET || 'thisneedstob3ch@ng3D';
const mongoose = require("mongoose");

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

const opts = {
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey: secret
};

module.exports = passport => {
  passport.use(
    new Strategy(opts, (payload, done) => {
      User.findOne({id: payload.id})
        .then(user => {
          if (user) {
            return done(null, {
              id: user.id,
              name: user.name,
              email: user.email
            });
          }
          return done(null, false);
        })
        .catch(err => log.error(err));
    })
  );
};

Regardless, I get the error. I've tried .findById() as well as .findOne()

Is there something I'm missing?

Upvotes: 1

Views: 1898

Answers (1)

Kerumen
Kerumen

Reputation: 4341

You made a typo in you user.js file, you forgot the s of module.exports:

module.exports = User = mongoose.model("User", UserSchema);

Upvotes: 3

Related Questions