From cca35fe9ac2fa25a410c61ba4b4555a563cf18d5 Mon Sep 17 00:00:00 2001 From: g-div Date: Mon, 29 Oct 2018 09:41:23 +0100 Subject: [PATCH] fix(relations): avoid duplicate entries in manyToMany relations --- .../strapi-hook-mongoose/lib/relations.js | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/packages/strapi-hook-mongoose/lib/relations.js b/packages/strapi-hook-mongoose/lib/relations.js index 0dcf8d35b7..8fc4832a1d 100644 --- a/packages/strapi-hook-mongoose/lib/relations.js +++ b/packages/strapi-hook-mongoose/lib/relations.js @@ -124,22 +124,11 @@ module.exports = { } virtualFields.push( - model.findOne({ - [model.primaryKey]: getValuePrimaryKey(value, model.primaryKey) + module.exports.addRelation.call(model, { + id: getValuePrimaryKey(value, this.primaryKey), + values: _.pick(value, [this.primaryKey, details.via]), + foreignKey: current }) - .populate([details.via]) - .lean() - .then(entry => { - if (_.isArray(entry[details.via])) { - value[details.via] = entry[details.via].map(entry => _.get(entry, '_id', entry)).concat(value[details.via]); - } - - return module.exports.addRelation.call(model, { - id: getValuePrimaryKey(value, this.primaryKey), - values: _.pick(value, [this.primaryKey, details.via]), - foreignKey: current - }); - }) ); });