我如何修复无效的架构配置在猫鼬和反应?



我试图用ReactJS创建一个基本的身份验证。我已经创建了一个模型与mongoose,但似乎有一个问题importing/accessing模型在我的控制器代码。

/模型/user.js

const mongoose = require('mongoose')
const crypto = require('crypto')
// user schema
const userSchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: true,
max: 32
},
email: {
type: String,
trim: true,
required: true,
unique: true,
lowercase: true
},
hashed_password: {
type: String,
required: true
},
salt: String,
role: {
type: String,
default: 'subscriber'
},
resetPasswordLink: {
data: String,
default: ''
},
}, {timestamps: true})
// virtual
userSchema.virtual('password')
.set(function(password) {
// code
})
.get(function() {
// code
})
// methods
userSchema.methods = {
// code
},
makeSalt: function() {
// code
}
};
module.exports = mongoose.model('User', userSchema)

/控制器/auth.js

const User = require('../models/user').schema; // import the user model
exports.signup = (req, res) => {
const{name, email, password} = req.body

User.findOne({email: email}).exec((err, user) => {
if (user) {
return res.status(400).json({
error: 'Email is taken'
})
}
})
let newUser = new User({name, email, password})
newUser.save((err, success) => {
if (err) {
console.log('SIGNUP ERROR', err)
return res.status(400).json({
error: err
})
}
res.json({
message: 'Signup sucess! Please signin'
})
})
}

问题:当我在终端运行npm start时,我看到这个错误:抛出新的TypeError(Invalid schema configuration: ${val} ' is not ' +

C:UsersjohnDocumentsreactmern-authenticationmern-auth-servernode_modulesmongooselibschema.js:677
throw new TypeError(`Invalid schema configuration: `${val}` is not ` +
^
TypeError: Invalid schema configuration: `` is not a valid type at path `default`. See mongoose-schematypes for a list of valid schema types.
at Schema.add (C:UsersjohnDocumentsreactmern-authenticationmern-auth-servermern-auth-servernode_modulesmongooselibschema.js:677:13)
at Schema.add (C:UsersjohnDocumentsreactmern-authenticationmern-auth-servermern-auth-servernode_modulesmongooselibschema.js:728:12)
at new Schema (C:UsersjohnDocumentsreactmern-authenticationmern-auth-servermern-auth-servernode_modulesmongooselibschema.js:134:10)
at Object.<anonymous> (C:UsersjohnDocumentsreactmern-authenticationmern-auth-servermodelsuser.js:5:20)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)

你们知道什么可以修复terminal error吗?非常感谢任何帮助。谢谢!

您在resetPasswordLink中有一个类型错误。将data: String改为type: String,如下图所示。

resetPasswordLink: {
type: String,
default: '',
},
//just this works for me
const User = require('../models/user'); 

相关内容

  • 没有找到相关文章

最新更新