英文:
How do I fix Invalid Schema Configuration in Mongoose and React?
问题
/models/user.js
const mongoose = require('mongoose')
const crypto = require('crypto')
// 用户模式
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 })
// 虚拟字段
userSchema.virtual('password')
.set(function(password) {
// 代码
})
.get(function() {
// 代码
})
// 方法
userSchema.methods = {
// 代码
makeSalt: function() {
// 代码
}
}
module.exports = mongoose.model('User', userSchema)
/controllers/auth.js
const User = require('../models/user').schema; // 导入用户模型
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('注册错误', err)
return res.status(400).json({
error: err
})
}
res.json({
message: '注册成功!请登录'
})
})
}
问题:当我在终端中运行 npm start
时,出现以下错误:throw new TypeError(Invalid schema configuration: \
${val}` is not +
C:\Users\john\Documents\react\mern-authentication\mern-auth-server\node_modules\mongoose\lib\schema.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:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:677:13)
at Schema.add (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:728:12)
at new Schema (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:134:10)
at Object.
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)
你知道如何修复终端错误吗?非常感谢任何帮助。谢谢!
<details>
<summary>英文:</summary>
I am trying to create a basic authentication with ReactJS. I have created a model with `mongoose` but there seems to be an issue `importing/accessing` the model in my controller code.
**/models/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)
**/controllers/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'
})
})
}
**Problem:** When I run `npm start` in the terminal, I see this error: **throw new TypeError(`Invalid schema configuration: \`${val}\` is not ` +**
C:\Users\john\Documents\react\mern-authentication\mern-auth-server\node_modules\mongoose\lib\schema.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:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:677:13)
at Schema.add (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:728:12)
at new Schema (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:134:10)
at Object.<anonymous> (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\models\user.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)
Do you guys know what can fix the `terminal error`? Any help is greatly appreciated. Thanks!
</details>
# 答案1
**得分**: 1
在`resetPasswordLink`中存在类型错误。
将`data: String` 更改为如下所示的`type: String`。
```javascript
resetPasswordLink: {
type: String,
default: '',
},
英文:
you have a type error in resetPasswordLink
.
instead of data: String
change to type: String
as shown below.
resetPasswordLink: {
type: String,
default: '',
},
答案2
得分: 1
//只需这部分对我有效
const User = require('../models/user');
英文:
//just this works for me
const User = require('../models/user');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论