Node.js mongoose model.findOne不是函数
模型有问题。正在尝试执行model.findOne(),但我一直收到错误消息 错误:Node.js mongoose model.findOne不是函数,node.js,mongoose,Node.js,Mongoose,模型有问题。正在尝试执行model.findOne(),但我一直收到错误消息 错误: TypeError: User.findOne(...).than is not a function 我肯定安装了mongoose,但似乎我不再适当地导入mongoose或用户模型了?不确定 User.js(模型) UserController.js const User = require('../models/User') myfunction(req, res) { const { name
TypeError: User.findOne(...).than is not a function
我肯定安装了mongoose,但似乎我不再适当地导入mongoose或用户模型了?不确定
User.js(模型)
UserController.js
const User = require('../models/User')
myfunction(req, res) {
const { name, email } = req.body
let checkEmail = await User.findOne({ email })
}
怎么了?您不是导出模型,而是导出用于生成模型的工厂(?)函数。只需删除:
module.exports = User => {
相反,请将您的return语句编辑为:
module.exports = mongoose.model('Users', UserSchema');
另外,请注意,通常最好以单数形式定义模型:
用户
,而不是用户
您不是导出模型,而是用于生成模型的工厂(?)函数。只需删除:
module.exports = User => {
相反,请将您的return语句编辑为:
module.exports = mongoose.model('Users', UserSchema');
另外,请注意,通常最好以单数形式定义您的模型:
用户
,而不是用户
您必须创建对模型的引用
,然后将其导出以用作对架构的引用
像这样,
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;
您必须创建对模型的引用
&然后导出该引用以用作对模式的引用
像这样,
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;