diff --git a/src/export/export.js b/src/export/export.js index c1ec6b7d4..e2dc9534c 100644 --- a/src/export/export.js +++ b/src/export/export.js @@ -153,7 +153,9 @@ module.exports = (router) => { // Array for promises const promises = []; - _.each(data, (field, key) => { + router.formio.util.eachComponentData(form.components, data, (component, data, row, path) => { + const field = _.get(data, path); + if (field && field._id) { // Add data property for resource fields promises.push( @@ -165,21 +167,20 @@ module.exports = (router) => { // Recurse for nested resources return addSubData(result.data) - .then(res => newData[key] = {data: res}); + .then(res => _.set(newData, path, {data: res})); }) .catch((error) => { debug(error); - newData[key] = field; + _.set(newData, path, field); }) ); } - else { - if (req.encryptedComponents && Object.keys(req.encryptedComponents).includes(key) && field) { - newData[key] = hook.alter('decrypt', req, field); - } - else { - newData[key] = field; - } + else if (component.encrypted && field) { + const result = hook.alter('decrypt', req, field); + _.set(newData, path, result); + } + else if (Object.keys(data).includes(path)) { + newData[path] = data[path]; } }); await Promise.all(promises); diff --git a/src/util/util.js b/src/util/util.js index d71c0ac60..d8576aa0a 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -200,6 +200,22 @@ const Utils = { */ eachComponent: Formio.Utils.eachComponent.bind(Formio.Utils), + /** + * Iterates through each component as well as its data. + * with the contextual data for that component in addition to the absolute path for that component. + * + * @param {Object} components + * The array of JSON components to iterate through. + * @param {Object} data + * The contextual data object for the components. + * @param {Function} fn + * The callback function to trigger for each component following the signature (component, data, row, path, components, index, parent). + * @param {Object} parent + * The parent component. + * @param {Boolean} includeAll + */ + eachComponentData: Formio.Utils.eachComponentData.bind(Formio.Utils), + /** * Get a component by its key *