File tree 1 file changed +22
-8
lines changed
1 file changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -51,15 +51,29 @@ export function writeResultToResponse(
51
51
// See https://github.com/loopbackio/loopback-next/issues/436
52
52
response . setHeader ( 'Content-Type' , 'application/json' ) ;
53
53
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 ] ;
60
74
} ) ;
61
- customResult . push ( org ) ;
62
- } ) ;
75
+ customResult = org ;
76
+ }
63
77
}
64
78
// TODO(bajtos) handle errors - JSON.stringify can throw
65
79
result = JSON . stringify ( customResult ) ;
You can’t perform that action at this time.
0 commit comments