如何修复Mongoose和React中的无效模式配置?

huangapple go评论112阅读模式
英文:

How do I fix Invalid Schema Configuration in Mongoose and React?

问题

/models/user.js

  1. const mongoose = require('mongoose')
  2. const crypto = require('crypto')
  3. // 用户模式
  4. const userSchema = new mongoose.Schema({
  5. name: {
  6. type: String,
  7. trim: true,
  8. required: true,
  9. max: 32
  10. },
  11. email: {
  12. type: String,
  13. trim: true,
  14. required: true,
  15. unique: true,
  16. lowercase: true
  17. },
  18. hashed_password: {
  19. type: String,
  20. required: true
  21. },
  22. salt: String,
  23. role: {
  24. type: String,
  25. default: 'subscriber'
  26. },
  27. resetPasswordLink: {
  28. data: String,
  29. default: ''
  30. },
  31. }, { timestamps: true })
  32. // 虚拟字段
  33. userSchema.virtual('password')
  34. .set(function(password) {
  35. // 代码
  36. })
  37. .get(function() {
  38. // 代码
  39. })
  40. // 方法
  41. userSchema.methods = {
  42. // 代码
  43. makeSalt: function() {
  44. // 代码
  45. }
  46. }
  47. module.exports = mongoose.model('User', userSchema)

/controllers/auth.js

  1. const User = require('../models/user').schema; // 导入用户模型
  2. exports.signup = (req, res) => {
  3. const { name, email, password } = req.body
  4. User.findOne({ email: email }).exec((err, user) => {
  5. if (user) {
  6. return res.status(400).json({
  7. error: 'Email is taken'
  8. })
  9. }
  10. })
  11. let newUser = new User({ name, email, password })
  12. newUser.save((err, success) => {
  13. if (err) {
  14. console.log('注册错误', err)
  15. return res.status(400).json({
  16. error: err
  17. })
  18. }
  19. res.json({
  20. message: '注册成功!请登录'
  21. })
  22. })
  23. }

问题:当我在终端中运行 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. (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)

你知道如何修复终端错误吗?非常感谢任何帮助。谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. **/models/user.js**
  5. const mongoose = require(&#39;mongoose&#39;)
  6. const crypto = require(&#39;crypto&#39;)
  7. // user schema
  8. const userSchema = new mongoose.Schema({
  9. name: {
  10. type: String,
  11. trim: true,
  12. required: true,
  13. max: 32
  14. },
  15. email: {
  16. type: String,
  17. trim: true,
  18. required: true,
  19. unique: true,
  20. lowercase: true
  21. },
  22. hashed_password: {
  23. type: String,
  24. required: true
  25. },
  26. salt: String,
  27. role: {
  28. type: String,
  29. default: &#39;subscriber&#39;
  30. },
  31. resetPasswordLink: {
  32. data: String,
  33. default: &#39;&#39;
  34. },
  35. }, {timestamps: true})
  36. // virtual
  37. userSchema.virtual(&#39;password&#39;)
  38. .set(function(password) {
  39. // code
  40. })
  41. .get(function() {
  42. // code
  43. })
  44. // methods
  45. userSchema.methods = {
  46. // code
  47. },
  48. makeSalt: function() {
  49. // code
  50. }
  51. };
  52. module.exports = mongoose.model(&#39;User&#39;, userSchema)
  53. **/controllers/auth.js**
  54. const User = require(&#39;../models/user&#39;).schema; // import the user model
  55. exports.signup = (req, res) =&gt; {
  56. const{name, email, password} = req.body
  57. User.findOne({email: email}).exec((err, user) =&gt; {
  58. if (user) {
  59. return res.status(400).json({
  60. error: &#39;Email is taken&#39;
  61. })
  62. }
  63. })
  64. let newUser = new User({name, email, password})
  65. newUser.save((err, success) =&gt; {
  66. if (err) {
  67. console.log(&#39;SIGNUP ERROR&#39;, err)
  68. return res.status(400).json({
  69. error: err
  70. })
  71. }
  72. res.json({
  73. message: &#39;Signup sucess! Please signin&#39;
  74. })
  75. })
  76. }
  77. **Problem:** When I run `npm start` in the terminal, I see this error: **throw new TypeError(`Invalid schema configuration: \`${val}\` is not ` +**
  78. C:\Users\john\Documents\react\mern-authentication\mern-auth-server\node_modules\mongoose\lib\schema.js:677
  79. throw new TypeError(`Invalid schema configuration: \`${val}\` is not ` +
  80. ^
  81. TypeError: Invalid schema configuration: `` is not a valid type at path `default`. See mongoose-schematypes for a list of valid schema types.
  82. at Schema.add (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:677:13)
  83. at Schema.add (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:728:12)
  84. at new Schema (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:134:10)
  85. at Object.&lt;anonymous&gt; (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\models\user.js:5:20)
  86. at Module._compile (node:internal/modules/cjs/loader:1254:14)
  87. at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
  88. at Module.load (node:internal/modules/cjs/loader:1117:32)
  89. at Module._load (node:internal/modules/cjs/loader:958:12)
  90. at Module.require (node:internal/modules/cjs/loader:1141:19)
  91. at require (node:internal/modules/cjs/helpers:110:18)
  92. Do you guys know what can fix the `terminal error`? Any help is greatly appreciated. Thanks!
  93. </details>
  94. # 答案1
  95. **得分**: 1
  96. 在`resetPasswordLink`中存在类型错误。
  97. 将`data: String` 更改为如下所示的`type: String`。
  98. ```javascript
  99. resetPasswordLink: {
  100. type: String,
  101. default: '',
  102. },
英文:

you have a type error in resetPasswordLink.
instead of data: String change to type: String as shown below.

  1. resetPasswordLink: {
  2. type: String,
  3. default: &#39;&#39;,
  4. },

答案2

得分: 1

  1. //只需这部分对我有效
  2. const User = require('../models/user');
英文:
  1. //just this works for me
  2. const User = require(&#39;../models/user&#39;);

huangapple
  • 本文由 发表于 2023年4月4日 18:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定