我有一個架構
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 是一個接受這些爭論并回傳令牌的函式。這是用于捕獲重復值的錯誤中間件
if(err.code === 11000){
const message = "Duplication Key Error"
error = new ErrorResponse(message, 400)
}
當我嘗試發送發布請求以創建供應商時,我收到重復密鑰錯誤。這是郵遞員 在此處輸入影像描述的結果
我不知道為什么會收到此錯誤
在此處輸入影像描述
從上圖中該欄位不存在,我已在我的架構和控制器中將該欄位名稱完全更改為行業型別,但我仍然收到索引:行業類別 1 復制鍵:{行業類別:空}“錯誤。問題可能是郵遞員還是什么?我應該重新安裝郵遞員嗎?我很困惑
uj5u.com熱心網友回復:
我過去遇到過同樣的問題,在我的情況下,我通過去mongo compass并從資料庫中洗掉用戶(在你的情況下是供應商)集合來解決問題。
如果您不想丟失供應商集合中的現有資料,您可以查詢所有供應商檔案并將它們作為 JSON 存盤在其他地方,洗掉供應商的集合,然后重新插入所有供應商的檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/437023.html
標籤:javascript 节点.js mongodb 猫鼬 猫鼬模式
