发送 post 请求时出现重复键错误
I am getting Duplication Key Error when I send a post request
我有一个架构
const VendorSchema = new mongoose.Schema({
firstName: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide firstName"],
trim: true
},
lastName: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide lastName"],
trim: true
},
businessName: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide your business name"],
// unique: true,
trim: true
},
businessLocation: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide your business location"],
trim: true
},
IDNumber: {
type: String,
minlength: [5, "This field requires a minimum of 5 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide ID Number or a passport"],
// unique: true,
trim: true
},
telephone: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide telephone"],
// unique: true,
trim: true
},
industryCategory: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please enter industry's category"],
trim: true
},
email: {
type: String,
minlength: [5, "This field requires a minimum of 5 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide an email"],
match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please provide a valid email address'],
// unique: true,
trim: true,
lowercase: true
},
password: {
type: String,
minlength: [5, "This field requires a minimum of 5 characters"],
maxlength: [1500, "This field requires a minimum of 1500 characters"],
required: [true, "Please provide password"],
},
resetPasswordToken : String,
resetPasswordExpiry : Date
})
这是我与上述架构相关的控制器
exports.vendorRegistration = async (req, res, next) => {
try {
const {
firstName,
lastName,
businessName,
businessLocation,
IDNumber,
industyCategory,
email,
telephone,
password
} = req.body
const vendor = await Vendor.create({
firstName,
lastName,
businessName,
businessLocation,
IDNumber,
industyCategory,
email,
telephone,
password
})
sendVendorToken(vendor, 201, res)
} catch (error) {
next(error)
}
}
sendVendorToken 是一个接收这些参数和 returns 令牌的函数。这是捕获重复值的错误中间件
if(err.code === 11000){
const message = "Duplication Key Error"
error = new ErrorResponse(message, 400)
}
当我尝试发送 post 请求来创建供应商时,我收到了重复密钥错误。这是 postman 的结果
enter image description here
我不知道为什么会收到此错误
enter image description here
从上图中该字段不存在我已经在我的模式和控制器中将该字段名称完全更改为 industryType 但我仍然得到索引:industryCategory_1 dup key:{ industryCategory:null }“错误。问题可能是 postman 还是什么?我应该重新安装 postman 吗?我很困惑
我过去遇到过同样的问题,就我而言,我通过转到 mongo compass 并从数据库中删除用户(在您的情况下是供应商)集合来解决问题。
如果您不想丢失供应商集合中的现有数据,您可以查询所有供应商文档并将它们存储在其他地方作为 JSON,删除供应商集合然后插入回所有供应商的文档
我有一个架构
const VendorSchema = new mongoose.Schema({
firstName: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide firstName"],
trim: true
},
lastName: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide lastName"],
trim: true
},
businessName: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide your business name"],
// unique: true,
trim: true
},
businessLocation: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide your business location"],
trim: true
},
IDNumber: {
type: String,
minlength: [5, "This field requires a minimum of 5 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide ID Number or a passport"],
// unique: true,
trim: true
},
telephone: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide telephone"],
// unique: true,
trim: true
},
industryCategory: {
type: String,
minlength: [3, "This field requires a minimum of 3 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please enter industry's category"],
trim: true
},
email: {
type: String,
minlength: [5, "This field requires a minimum of 5 characters"],
maxlength: [50, "This field requires a maximum of 50 characters"],
required: [true, "Please provide an email"],
match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please provide a valid email address'],
// unique: true,
trim: true,
lowercase: true
},
password: {
type: String,
minlength: [5, "This field requires a minimum of 5 characters"],
maxlength: [1500, "This field requires a minimum of 1500 characters"],
required: [true, "Please provide password"],
},
resetPasswordToken : String,
resetPasswordExpiry : Date
})
这是我与上述架构相关的控制器
exports.vendorRegistration = async (req, res, next) => {
try {
const {
firstName,
lastName,
businessName,
businessLocation,
IDNumber,
industyCategory,
email,
telephone,
password
} = req.body
const vendor = await Vendor.create({
firstName,
lastName,
businessName,
businessLocation,
IDNumber,
industyCategory,
email,
telephone,
password
})
sendVendorToken(vendor, 201, res)
} catch (error) {
next(error)
}
}
sendVendorToken 是一个接收这些参数和 returns 令牌的函数。这是捕获重复值的错误中间件
if(err.code === 11000){
const message = "Duplication Key Error"
error = new ErrorResponse(message, 400)
}
当我尝试发送 post 请求来创建供应商时,我收到了重复密钥错误。这是 postman 的结果 enter image description here
我不知道为什么会收到此错误
enter image description here
从上图中该字段不存在我已经在我的模式和控制器中将该字段名称完全更改为 industryType 但我仍然得到索引:industryCategory_1 dup key:{ industryCategory:null }“错误。问题可能是 postman 还是什么?我应该重新安装 postman 吗?我很困惑
我过去遇到过同样的问题,就我而言,我通过转到 mongo compass 并从数据库中删除用户(在您的情况下是供应商)集合来解决问题。
如果您不想丢失供应商集合中的现有数据,您可以查询所有供应商文档并将它们存储在其他地方作为 JSON,删除供应商集合然后插入回所有供应商的文档