diff --git a/packages/strapi-hook-mongoose/lib/index.js b/packages/strapi-hook-mongoose/lib/index.js index 3829faed44..b2cc899a6a 100644 --- a/packages/strapi-hook-mongoose/lib/index.js +++ b/packages/strapi-hook-mongoose/lib/index.js @@ -174,7 +174,10 @@ module.exports = function (strapi) { _.forEach(postLifecycle, (fn, key) => { if (_.isFunction(target[model.toLowerCase()][fn])) { collection.schema.post(key, function (doc, next) { - target[model.toLowerCase()][fn](this, doc).then(next).catch(err => strapi.log.error(err)); + target[model.toLowerCase()][fn](this, doc).then(next).catch(err => { + strapi.log.error(err); + next(err); + }); }); } }); diff --git a/packages/strapi-plugin-content-manager/config/queries/mongoose.js b/packages/strapi-plugin-content-manager/config/queries/mongoose.js index e5efe8f7db..b6104c9cac 100644 --- a/packages/strapi-plugin-content-manager/config/queries/mongoose.js +++ b/packages/strapi-plugin-content-manager/config/queries/mongoose.js @@ -106,11 +106,13 @@ module.exports = { const request = await this.create(values) .catch((err) => { - const message = err.message.split('index:'); - const field = _.words(_.last(message).split('_')[0]); - const error = { message: `This ${field} is already taken`, field }; - - throw error; + if (err.message) + { + const message = err.message.split('index:'); + const field = _.words(_.last(message).split('_')[0]); + err = { message: `This ${field} is already taken`, field }; + } + throw err; }); // Transform to JSON object.