Skip to content

Commit f268cb8

Browse files
committed
fix: fix
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 24cce3f commit f268cb8

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

packages/rest/src/writer.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,29 @@ export function writeResultToResponse(
5151
// See https://github.com/loopbackio/loopback-next/issues/436
5252
response.setHeader('Content-Type', 'application/json');
5353
let customResult = result;
54-
if (result.length) {
55-
customResult = [];
56-
result.forEach((item: {[key: string]: Object[]}) => {
57-
const org: {[key: string]: Object[]} = {};
58-
Object.keys(item).forEach(key => {
59-
org[key] = item[key];
54+
let org: {[key: string]: Object[]} = {};
55+
56+
if (result && typeof result === 'object') {
57+
if (Array.isArray(result)) {
58+
customResult = [];
59+
result.forEach((item: {[key: string]: Object[]}) => {
60+
org = {};
61+
if (typeof item === 'object') {
62+
Object.keys(item).forEach(key => {
63+
org[key] = item[key];
64+
});
65+
customResult.push(org);
66+
} else {
67+
customResult.push(item);
68+
}
69+
});
70+
} else {
71+
org = {};
72+
Object.keys(result).forEach(key => {
73+
org[key] = result[key];
6074
});
61-
customResult.push(org);
62-
});
75+
customResult = org;
76+
}
6377
}
6478
// TODO(bajtos) handle errors - JSON.stringify can throw
6579
result = JSON.stringify(customResult);

0 commit comments

Comments
 (0)