Rafael Pelizza
Rafael Pelizza

Reputation: 11

Node.js mongoose model.findOne is not a function

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

Error:

TypeError: User.findOne(...).than is not a function

I definitely installed mongoose, but it seems I am no longer importing mongoose or the User model appropriately? Not sure.

User.js (model)

const mongoose = require('mongoose')
const Schema = mongoose.Schema

module.exports = User => {
    var UserSchema = new Schema({    
        name: String,
        email: { type: String, unique: true },
        password: String,
        passwordResetToken: String,
        passwordResetExpires: Date,
        document: String,
        profile_picture: String,
        ocupation: { type: Schema.Types.ObjectId, ref: 'Ocupation' },
        is_admin: { type: Boolean, default: false },
        sector: { type: Schema.Types.ObjectId, ref: 'Sector' },
        is_manager: { type: Boolean, default: false },
        group: { type: Schema.Types.ObjectId, ref: 'Group' },
        is_team_leader: { type: Boolean, default: false },
        can_start: { type: Boolean, default: true },
        isVerified: { type: Boolean, default: false },
        created_at: { type: Date, default: Date.now },
        updated_at: { type: Date, default: Date.now },
        deleted_at: Date,
    }, {
        toJSON: {
            virtuals: true
        }
    })

    UserSchema.virtual('profile_url').get(function() {
        return `http://${process.env.HOST}:${process.env.NODE_ENV ? process.env.DEV_PORT : process.env.PORT}/3e002f70cbf8805c904bf1536a22a52e/${this.profile_picture}`
    })

    return mongoose.model('Users', UserSchema)
}

UserController.js

const User = require('../models/User')

myfunction(req, res) {
    const { name, email } = req.body

    let checkEmail = await User.findOne({ email })
}

whats wrong?

Upvotes: 0

Views: 291

Answers (2)

Sarfraaz
Sarfraaz

Reputation: 1269

You'll have to create reference to your model & then export that to be used as reference to the Schema

Like this,

const mongoose = require('mongoose')
const Schema = mongoose.Schema

    var UserSchema = new Schema({    
        name: String,
        email: { type: String, unique: true },
        password: String,
        passwordResetToken: String,
        passwordResetExpires: Date,
        document: String,
        profile_picture: String,
        ocupation: { type: Schema.Types.ObjectId, ref: 'Ocupation' },
        is_admin: { type: Boolean, default: false },
        sector: { type: Schema.Types.ObjectId, ref: 'Sector' },
        is_manager: { type: Boolean, default: false },
        group: { type: Schema.Types.ObjectId, ref: 'Group' },
        is_team_leader: { type: Boolean, default: false },
        can_start: { type: Boolean, default: true },
        isVerified: { type: Boolean, default: false },
        created_at: { type: Date, default: Date.now },
        updated_at: { type: Date, default: Date.now },
        deleted_at: Date,
    }, {
        toJSON: {
            virtuals: true
        }
    })

    UserSchema.virtual('profile_url').get(function() {
        return `http://${process.env.HOST}:${process.env.NODE_ENV ? process.env.DEV_PORT : process.env.PORT}/3e002f70cbf8805c904bf1536a22a52e/${this.profile_picture}`
    })

    var User = mongoose.model('Users', UserSchema)

module.exports = User;

Upvotes: 0

technophyle
technophyle

Reputation: 9159

You're not exporting the model, but a factory(?) function for generating a model. Just remove:

module.exports = User => {

and instead, edit your return statement to:

module.exports = mongoose.model('Users', UserSchema');

Also, side note, it's usually a good idea to define your model in a singular form: User, not Users.

Upvotes: 2

Related Questions