首页 > 解决方案 > 为什么此错误显示“非法参数:未定义,字符串”?

问题描述

我正在构建一个简单的 node.js 应用程序。我为用户注册构建了我的后端 api。我正在尝试使用邮递员对其进行测试,但出现此错误“非法参数:未定义,字符串”。什么可以为此负责?相关代码如下

用户模式

const mongoose = require('mongoose');
const Schema = mongoose.Schema

const UserSchema = new Schema({

    userName: {
        type: String,
        required: true,
        unique: true

    },
    firstName: {
        type: String,
    },
    lastName: {
        type: String,
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    dateOfRegistration: {
        type: Date,
        default: Date.now
       
    },
    dateOfBirth: {
        type: Date,
    },
     userCategory: {
        type: String,
        default: 'workingClass'
       
    }

})
module.exports = mongoose.model('users', UserSchema)


标签: javascriptnode.jsmongodbexpressmongoose

解决方案


问题已解决。Postman 以“文本”格式而不是“JSON”格式发送请求,因此后端无法理解数据。将我的邮递员的设置从“文本”更改为“JSON”时,一切正常。


推荐阅读