diff --git a/packages/rest/src/writer.ts b/packages/rest/src/writer.ts index c4db2a0f3d15..81782436843f 100644 --- a/packages/rest/src/writer.ts +++ b/packages/rest/src/writer.ts @@ -50,42 +50,32 @@ export function writeResultToResponse( // TODO(ritch) remove this, should be configurable // See https://github.com/loopbackio/loopback-next/issues/436 response.setHeader('Content-Type', 'application/json'); - // let customResult = result; - // let org: {[key: string]: Object[]} = {}; - // if (result && typeof result === 'object') { - // if (Array.isArray(result)) { - // customResult = []; - // result.forEach((item: {[key: string]: Object[]}) => { - // org = {}; - // if (typeof item === 'object') { - // Object.keys(item).forEach(key => { - // org[key] = item[key]; - // }); - // customResult.push(org); - // } else { - // customResult.push(item); - // } - // }); - // } else { - // org = {}; - // Object.keys(result).forEach(key => { - // console.log('-----------------'); - // console.log(key); - // console.log('-----------------'); - // org[key] = result[key]; - // }); - // customResult = org; - // } - // } + let customResult = result; + let org: {[key: string]: Object[]} = {}; + if (result && typeof result === 'object') { + if (Array.isArray(result)) { + customResult = []; + result.forEach((item: {[key: string]: Object[]}) => { + org = {}; + if (typeof item === 'object') { + Object.keys(item).forEach(key => { + org[key] = item[key]; + }); + customResult.push(org); + } else { + customResult.push(item); + } + }); + } else { + org = {}; + Object.keys(result).forEach(key => { + org[key] = result[key]; + }); + customResult = org; + } + } // TODO(bajtos) handle errors - JSON.stringify can throw - // result = JSON.stringify(customResult); - console.log('------------before------------'); - console.log(result); - console.log('------------before------------'); - result = JSON.stringify(result); - console.log('------------after------------'); - console.log(result); - console.log('------------after------------'); + result = JSON.stringify(customResult); } break; default: