From 83b032e9b360c106557a1286fc552f09058fa0c3 Mon Sep 17 00:00:00 2001 From: Jim LAURIE Date: Fri, 20 Jul 2018 17:20:06 +0200 Subject: [PATCH] Fix udpate file SQL via content manager fix #1461 --- packages/strapi-hook-bookshelf/lib/relations.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/strapi-hook-bookshelf/lib/relations.js b/packages/strapi-hook-bookshelf/lib/relations.js index 57ab4bf757..4b3083bad9 100644 --- a/packages/strapi-hook-bookshelf/lib/relations.js +++ b/packages/strapi-hook-bookshelf/lib/relations.js @@ -10,15 +10,17 @@ const _ = require('lodash'); // Utils const { models: { getValuePrimaryKey } } = require('strapi-utils'); -const transformToArrayID = (array) => { +const transformToArrayID = (array, association) => { if(_.isArray(array)) { - return array.map(value => { + array = array.map(value => { if (_.isPlainObject(value)) { - return value._id || value.id; + return value._id || value.id || false; } return value; }); + + return array.filter(n => n); } if (association.type === 'model' || (association.type === 'collection' && _.isObject(array))) { @@ -140,8 +142,8 @@ module.exports = { case 'manyToMany': if (response[current] && _.isArray(response[current]) && current !== 'id') { // Compare array of ID to find deleted files. - const currentValue = transformToArrayID(response[current]).map(id => id.toString()); - const storedValue = transformToArrayID(params.values[current]).map(id => id.toString()); + const currentValue = transformToArrayID(response[current], association).map(id => id.toString()); + const storedValue = transformToArrayID(params.values[current], association).map(id => id.toString()); const toAdd = _.difference(storedValue, currentValue); const toRemove = _.difference(currentValue, storedValue); @@ -229,8 +231,8 @@ module.exports = { case 'oneToManyMorph': case 'manyToManyMorph': { // Compare array of ID to find deleted files. - const currentValue = transformToArrayID(response[current]).map(id => id.toString()); - const storedValue = transformToArrayID(params.values[current]).map(id => id.toString()); + const currentValue = transformToArrayID(response[current], association).map(id => id.toString()); + const storedValue = transformToArrayID(params.values[current], association).map(id => id.toString()); const toAdd = _.difference(storedValue, currentValue); const toRemove = _.difference(currentValue, storedValue);