diff --git a/lib/helpers/montaineType.js b/lib/helpers/montaineType.js index c99a5d114..357d422bd 100644 --- a/lib/helpers/montaineType.js +++ b/lib/helpers/montaineType.js @@ -17,9 +17,9 @@ const typingTypes = require(path.resolve('./lib/helpers/typing/types')); * @return {data} data - new data generated */ const format = (data, action, object) => { - if (!data) throw new AppError('Typing : format data not defined.', { code: 'HELPERS_ERROR' }); + if (!data) throw new AppError(`Typing : format data not defined : ${data}`, { code: 'HELPERS_ERROR' }); if (!action) return data; - if (!object) throw new AppError('Typing : format object not defined.', { code: 'HELPERS_ERROR' }); + if (!object) throw new AppError(`Typing : format object not defined : ${object}`, { code: 'HELPERS_ERROR' }); const func = action.split('(')[0]; const params = action.split('(')[1].slice(0, -1).split(','); @@ -70,7 +70,8 @@ const prepareFormat = (object, schema) => { const keys = tricks.objectDeepKeys(object); // get all keys keys.forEach((k) => { const value = _.get(object, k); - if (!Array.isArray(value) && typeof value !== 'object') { + if (!value || value === '') _.unset(object, k); + else if (!Array.isArray(value) && typeof value !== 'object') { const p = k.split('.'); let func; if (/^\d+$/.test(p[p.length - 1])) { // array diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index 233cb58aa..ddf0d43b8 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -110,19 +110,19 @@ exports.load = async (api, start) => { result.result = request.data.result; // Mapping if (result.result && api.mapping && api.mapping !== '') { - result.mapping = montaineMap.map(result.result, JSON.parse(api.mapping)); + result.mapping = montaineMap.map(_.cloneDeep(result.result), JSON.parse(api.mapping)); result.result = result.mapping; } // Typing if (result.result && api.typing && api.typing !== '') { - result.typing = montaineType.type(result.result, JSON.parse(api.typing)); + result.typing = montaineType.type(_.cloneDeep(result.result), JSON.parse(api.typing)); result.result = result.typing; } // prepare for save if (result.result) { - result.prepare = montaineSave.prepare(result.result, start); - result.mongo = montaineSave.save(result.prepare, start); + result.prepare = montaineSave.prepare(_.cloneDeep(result.result), start); + result.mongo = montaineSave.save(_.cloneDeep(result.prepare), start); result.result = result.mongo; if (api.savedb) result.result = await ApisRepository.import(api.slug, result.result); }