首页 > 解决方案 > UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: db.footballers index: data_1 dup key: { data: null }

问题描述

我以前从未遇到过这个问题。我一直在轻松创建实例。但是有了这个项目,它给我带来了关于索引或其他东西的错误。我什至不知道那是什么。先感谢您。

这是我的架构

const mongoose = require("mongoose");

const footballersSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: true
    }
})

module.exports = mongoose.model('footballers', footballersSchema);

并且只是触发创建新实例的简单控制器

exports.checkUserForReg = async function(req, res, next){
            await footballersModel.create(req.body);
            res.(201).json({status: "Ok"})
}

但它给了我错误UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: db.footballers index: data_1 dup key: { data: null }

是的,每个实例都应该有唯一的名称字段,但即使我尝试不同,它也会给我这个错误。我看到错误提示,但我的架构中index: data_1 dup key: { data: null }根本没有密钥!index那是什么 ?谢谢!

标签: expressmongoose

解决方案


推荐阅读