我正在尝试创建问题跟踪器。其中包含以下结构。
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const time = require('../libs/timeLib')
let newRecord = new Schema(
{
title:{type: String, default: ''},
issueType: [],
description: {type: String},
issueLocation: {type: String},
createdBy: {type: String, index: true},
issueId: {type: String, unique: true},
issueCreatedOn: {type: Date, default: time.now()},
status: {
open: {type: Boolean, default: false},
closed: {type: Boolean, default: false}
},
screenshots: []
}
)
module.exports = mongoose.model('Record', newRecord)
我编写了以下代码来创建新记录。我能够第一次创建记录。 但是当我第二次尝试使用相同的createdBy创建相同的东西时。我收到以下错误 我正在使用Postman与API进行交互
E11000重复键错误集合:issueTrackingDB.records索引: userId_1重复键:{:null}
在下面查看为创建而创建的功能
let createIssue = (req, res)=>{
let createNewRecord = () =>{
return new Promise((resolve, reject)=>{
let newIssue = new RecordModel({
title: req.body.title,
description: req.body.description,
issueLocation: req.body.issueLocation,
createdBy: req.body.fullName,
issueId: shortid.generate(),
issueCreatedOn: time.now(),
})
let issueType = (req.body == undefined || req.body.issueType == null || req.body.issueType == '')? []: req.body.issueType.split(',')
newIssue.issueType = issueType
let screenshots = (req.body == undefined || req.body.screenshots == null || req.body.screenshots == '')? []: req.body.screenshots.split(',')
newIssue.screenshots = screenshots
newIssue.save((err, issueDetails)=>{
if(err){
logger.error(err.message, ' recordsController, createIssue', 8)
let apiResponse = response.generate(true, "Failed To Create Issue", 500, null)
reject(apiResponse)
} else {
issueDetails.status.open = true
resolve(issueDetails)
}
})
})
} // end create new record
createNewRecord((resolve)=>{
console.log("Issue Created Successfully")
let apiResponse = response.generate(false, "Issue Created Successfully",
200, resolve)res.send(apiResponse)
})
.catch((err)=>{
console.log(err)
console.log("Some Error occured")
res.send(err)
})
} // end create new issue
答案 0 :(得分:0)
您在“ issueId:{type:String,unique:true}”上具有唯一键,请确保每次调用时值都更改或删除唯一键
答案 1 :(得分:-1)
为自己找到了解决方案。谢谢