Node.js猫鼬model.findOne不是函数

问题描述 投票:0回答:2

模型有问题。尝试做一个model.findOne(),但我一直收到错误消息

错误:

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

我确实安装了猫鼬,但是似乎我不再适当地导入猫鼬或用户模型了?不确定。

User.js(模型)

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 })
}

怎么了?

node.js mongoose
2个回答
1
投票

您不是要导出模型,而是要使用factory(?)函数来生成模型。只需删除:

module.exports = User => {

相反,将您的return语句编辑为:

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

此外,请注意,通常最好以单数形式定义模型:User,而不是Users


0
投票

您必须创建对model的引用,然后将其导出以用作对Schema的引用

喜欢这个,

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;
© www.soinside.com 2019 - 2024. All rights reserved.