首页 > 解决方案 > 我在带有猫鼬的节点 js 中遇到此错误,E11000 重复键错误集合:domesticIndia.pans index: pan_id_1 dup key: { pan_id: null }

问题描述

我的架构

post方法使用我这个错误来了,为此我使用mongoose和node js。如何解决此错误

E11000 重复键错误集合:domesticIndia.pans 索引:pan_id_1 重复键:{ pan_id:null } 虽然我没有制作任何名为 pans 的集合。我已经很好地检查了这一点

我的架构

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

const panSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    areaoffice: {
        type: String,

    },
    aocode: {
        type: String,

    },
    ao: {
        type: String,

    },
    range: {
        type: String,

    },
    aonumber: {
        type: String,

    },
    application: {
        type: String,

    },
    old_pan: {
        type: String,

    },
    category: {
        type: String,

    },
    applicant: {
        type: String,

    },
    firstname: {
        type: String,

    },
    middlename: {
        type: String,

    },
    lastname: {
        type: String,

    },
    ffirstname: {
        type: String,

    },
    fmiddlename: {
        type: String,

    },
    flastname: {
        type: String,

    },
    mfirstname: {
        type: String,

    },
    mmiddlename: {
        type: String,

    },
    mlastname: {
        type: String,

    },
    cardHolder: {
        type: String,

    },
    dob: {
        type: String,

    },
    contect_number: {
        type: String,

    },
    email: {
        type: String,

    },
    proofid: {
        type: String,

    },
    proofadd: {
        type: String,

    },
    proofdob: {
        type: String,

    },
    gender: {
        type: String,

    },
    adhar_number: {
        type: String,

    },
    address_f: {
        type: String,

    },
    address_v: {
        type: String,

    },
    address_p: {
        type: String,

    },
    address_divi: {
        type: String,

    },
    address_d: {
        type: String,

    },
    state: {
        type: String,

    },
    pin_code: {
        type: String,

    },
    image: {
        type: String,

    },
    sig: {
        type: String
    },
    pdf: {
        type: String
    },
    username: {
        type: String
    },
    avatar: {
        type: String
    },
    date: {
        type: Date,
        default: Date.now
    }
})

module.exports = Pan = mongoose.model('pan', panSchema)

发布方法代码

// @route    POST pan/newpan
// @desc     Create a post
// @access   Private
router.post(
  '/newpan',
  [
    auth,
    [
      check('areaoffice', 'Area Office is required')
        .not()
        .isEmpty(),
      check('aocode', 'aocode is required')
        .not()
        .isEmpty(),
      check('ao', 'Ao is required')
        .not()
        .isEmpty(),
      check('range', 'Range is required')
        .not()
        .isEmpty(),
      check('aonumber', 'AO number is required')
        .not()
        .isEmpty(),
      check('application', 'Application Type is required')
        .not()
        .isEmpty(),
      check('old_pan', 'Old Pan Type is required')
        .not()
        .isEmpty(),
      check('category', 'Category Type is required')
        .not()
        .isEmpty(),
      check('applicant', 'Applicant Type is required')
        .not()
        .isEmpty(),
      check('firstname', 'First name is required')
        .not()
        .isEmpty(),
      check('middlename', 'Middle Name is required')
        .not()
        .isEmpty(),
      check('lastname', 'Last Name is required')
        .not()
        .isEmpty(),
      check('ffirstname', 'Father first Name is required')
        .not()
        .isEmpty(),
      check('fmiddlename', 'Father Middle Name is required')
        .not()
        .isEmpty(),
      check('flastname', 'Father Last Name is required')
        .not()
        .isEmpty(),
      check('mfirstname', 'Mother First Name is required')
        .not()
        .isEmpty(),
      check('mmiddlename', 'Mother Middle Name is required')
        .not()
        .isEmpty(),
      check('mlastname', 'Mother Last Name is required')
        .not()
        .isEmpty(),
      check('cardHolder', 'Card Holder Name is required')
        .not()
        .isEmpty(),
      check('dob', 'Date of Birth is required')
        .not()
        .isEmpty(),
      check('contect_number', 'Contact Number is required')
        .not()
        .isEmpty(),
      check('email', 'Email is required')
        .not()
        .isEmpty(),
      check('proofid', 'Id Proof is required')
        .not()
        .isEmpty(),
      check('proofadd', 'Id Address is required')
        .not()
        .isEmpty(),
      check('proofdob', 'Id Date of Birth is required')
        .not()
        .isEmpty(),
      check('gender', 'Gender is required')
        .not()
        .isEmpty(),
      check('adhar_number', 'Adhar Number is required')
        .not()
        .isEmpty(),
      check('address_f', 'Address is required')
        .not()
        .isEmpty(),
      check('address_v', 'Address is required')
        .not()
        .isEmpty(),
      check('address_p', 'Post office Address is required')
        .not()
        .isEmpty(),
      check('address_divi', 'Address of Division is required')
        .not()
        .isEmpty(),
      check('address_d', 'Address of Dist. is required')
        .not()
        .isEmpty(),
      check('state', 'State is required')
        .not()
        .isEmpty(),
      check('pin_code', 'Pin Code is required')
        .not()
        .isEmpty()
    ]
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ msg: errors.array() });
    }

    try {
      const user = await users.findById(req.user.id).select('-password');
      let image = req.files.image;
      let pdf = req.files.pdf;
      let sig = req.files.sig;
      const newPan = new Pan({
        areaoffice: req.body.areaoffice,
        aocode: req.body.aocode,
        ao: req.body.ao,
        range: req.body.range,
        aonumber: req.body.aonumber,
        application: req.body.application,
        old_pan: req.body.old_pan,
        category: req.body.category,
        applicant: req.body.applicant,
        firstname: req.body.firstname,
        middlename: req.body.middlename,
        lastname: req.body.lastname,
        ffirstname: req.body.ffirstname,
        fmiddlename: req.body.fmiddlename,
        flastname: req.body.flastname,
        mfirstname: req.body.mfirstname,
        mmiddlename: req.body.mmiddlename,
        mlastname: req.body.mlastname,
        cardHolder: req.body.cardHolder,
        dob: req.body.dob,
        contect_number: req.body.contect_number,
        email: req.body.email,
        proofid: req.body.proofid,
        proofadd: req.body.proofadd,
        proofdob: req.body.proofdob,
        gender: req.body.gender,
        adhar_number: req.body.adhar_number,
        address_f: req.body.address_f,
        address_v: req.body.address_v,
        address_p: req.body.address_p,
        address_divi: req.body.address_divi,
        address_d: req.body.address_d,
        state: req.body.state,
        pin_code: req.body.pin_code,
        image: image.name,
        pdf: pdf.name,
        sig: sig.name,
        imagepath: image.tempFilePath,
        username: user.username,
        avatar: user.avatar,
        user: req.user.id
      });

      image.mv(`./client/public/panImages/${image.name}`, function (err) {
        if (err) {
          return res.status(500).json({ msg: 'something Error' });
        }
      })
      sig.mv(`./client/public/panImages/${sig.name}`, function (err) {
        if (err) {
          return res.status(500).json({ msg: 'something Error' });
        }
      })
      pdf.mv(`./client/public/panImages/${pdf.name}`, function (err) {
        if (err) {
          return res.status(500).json({ msg: 'something Error' });
        }
      })
      const pan = await newPan.save();
      res.json({ panData: pan, msg: 'Pan Created' });
    } catch (err) {
      console.error(err.message);
      res.status(500).send('Server Error');
    }
  }
);

标签: node.jsexpressmongoosereduxreact-redux

解决方案


根据这个答案,您似乎有一个已经被删除的pan_id字段unique,但索引没有被删除。

在 mongo shell 上,您可以使用以下方法删除索引:

db.pans.dropIndex({"pan_id":1})

推荐阅读