File tree Expand file tree Collapse file tree 1 file changed +28
-4
lines changed
Expand file tree Collapse file tree 1 file changed +28
-4
lines changed Original file line number Diff line number Diff line change 11// catchAwait.js
2- const catchAwait = promise => (
2+ const catchAwait = promise =>
33 promise
44 . then ( data => ( { data, error : null } ) )
5- . catch ( error => ( { error, data : null } ) )
6- ) ;
5+ . catch ( error => ( { error, data : null } ) ) ;
76
87module . exports = catchAwait ;
98
@@ -17,6 +16,31 @@ const allItems = async () => {
1716 // code
1817 }
1918 console . error ( error ) ;
20- }
19+ } ;
2120
2221allItems ( ) ;
22+
23+ /**
24+ * Another way
25+ */
26+
27+ // catchAsync.js
28+ module . exports = fn => {
29+ return ( req , res , next ) => {
30+ fn ( req , res , next ) . catch ( next ) ;
31+ } ;
32+ } ;
33+
34+ // createOne.js
35+
36+ exports . createOne = Model =>
37+ catchAsync ( async ( req , res , next ) => {
38+ const doc = await Model . create ( req . body ) ;
39+
40+ res . status ( 201 ) . json ( {
41+ status : 'success' ,
42+ data : {
43+ data : doc ,
44+ } ,
45+ } ) ;
46+ } ) ;
You can’t perform that action at this time.
0 commit comments