Mongoose unique index error logs and other index error logs

This commit is contained in:
Alexandre Bodin 2019-08-09 18:25:44 +02:00
parent 664491d593
commit e1a319a25e
2 changed files with 15 additions and 0 deletions

View File

@ -8,6 +8,7 @@
"type": {
"type": "string",
"required": true,
"unique": true,
"configurable": true
}
}

View File

@ -272,6 +272,20 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
definition.collectionName
);
Model.on('index', error => {
if (error) {
if (error.code === 11000) {
strapi.log.error(
`Unique constraint fails, make sure to update your data and restart to apply the unique constraint.\n\t- ${error.message}`
);
} else {
strapi.log.error(
`An index error happened, it wasn't applied.\n\t- ${error.message}`
);
}
}
});
if (!plugin) {
global[definition.globalName] = Model;
}