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

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

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. (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)

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


<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(&#39;mongoose&#39;)
    const crypto = require(&#39;crypto&#39;)
    
    // 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: &#39;subscriber&#39;
      },
      resetPasswordLink: {
        data: String,
        default: &#39;&#39;
      },
    }, {timestamps: true})
    
    // virtual
    userSchema.virtual(&#39;password&#39;)
    .set(function(password) {
      // code
    })
    .get(function() {
      // code
    })
    
    // methods
    userSchema.methods = {
        // code
      },
    
      makeSalt: function() {
        // code
      }
    };
    
    module.exports = mongoose.model(&#39;User&#39;, userSchema)

**/controllers/auth.js**

    const User = require(&#39;../models/user&#39;).schema; // import the user model
    
    exports.signup = (req, res) =&gt; {
      const{name, email, password} = req.body
      
      User.findOne({email: email}).exec((err, user) =&gt; {
        if (user) {
          return res.status(400).json({
            error: &#39;Email is taken&#39;
          })
        }
      })
    
      let newUser = new User({name, email, password})
    
      newUser.save((err, success) =&gt; {
        if (err) {
          console.log(&#39;SIGNUP ERROR&#39;, err)
    
          return res.status(400).json({
            error: err
          })
        }
    
        res.json({
          message: &#39;Signup sucess! Please signin&#39;
        })
      })
    }

**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.&lt;anonymous&gt; (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: &#39;&#39;,
        },

答案2

得分: 1

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

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