簡體   English   中英

發送發布請求時出現復制鍵錯誤

[英]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
})

這是我與上述架構相關的 controller

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 是一個 function,它接收這些參數並返回一個令牌。 這是捕獲重復值的錯誤中間件

    if(err.code === 11000){
    const message = "Duplication Key Error"
    error = new ErrorResponse(message, 400)
}

當我嘗試發送發布請求以創建供應商時,出現重復鍵錯誤。 這是 postman 的結果enter image description here

我不知道為什么會收到此錯誤

在此處輸入圖像描述

從上圖中該字段不存在我已經在我的模式和控制器中將該字段名稱完全更改為 industryType 但我仍然得到 index: industryCategory_1 dup key: { industryCategory: null }" 錯誤。問題可能是 postman 還是什么? 我應該重新安裝 postman 嗎?我很困惑

我過去遇到過同樣的問題,就我而言,我通過轉到mongo compass並從數據庫中刪除用戶(在你的情況下是供應商)集合來解決問題。

如果您不想丟失供應商集合中的現有數據,您可以查詢所有供應商文檔並將它們存儲在其他地方,如 JSON,刪除供應商集合,然后插入所有供應商文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM