@@ -69,14 +69,18 @@ module.exports = function (RED) {
6969 function determineSuffix ( msg ) {
7070 // Let's assume that if we can't determine the suffix that
7171 // its a word doc.
72- var ext = '.json' ,
73- ft = fileType ( msg . payload ) ;
72+ let ext = '.json' ;
73+ if ( ! discoveryutils . isJsonObject ( msg . payload ) ) {
74+ let ext = '.json' ,
75+ ft = fileType ( msg . payload ) ;
7476
75- if ( ft && ft . ext ) {
76- ext = '.' + ft . ext ;
77- }
78- if ( isDocx ( msg . payload ) ) {
79- ext = '.docx' ;
77+ if ( ft && ft . ext ) {
78+ ext = '.' + ft . ext ;
79+ }
80+
81+ if ( isDocx ( msg . payload ) ) {
82+ ext = '.docx' ;
83+ }
8084 }
8185
8286 return Promise . resolve ( ext ) ;
@@ -131,20 +135,22 @@ module.exports = function (RED) {
131135 // modify as getting addJsonDocument will be deprecated messages
132136 if ( '.json' === suffix ) {
133137 //method = 'addJsonDocument';
134- params . file = JSON . stringify ( params . file ) ;
138+ //params.file = JSON.stringify(params.file);
139+
140+ params . file = Buffer . from ( JSON . stringify ( params . file ) ) ;
135141 //} else {
136142 //method = 'addDocument';
137143 }
138144 method = 'addDocument' ;
139145
140- discovery [ method ] ( params , function ( err , response ) {
141- if ( err ) {
142- reject ( err ) ;
143- } else {
144- msg . document = response . document ? response . document : response ;
146+ discovery [ method ] ( params )
147+ . then ( ( response ) => {
148+ msg . document = response . result ? response . result : response ;
145149 resolve ( ) ;
146- }
147- } ) ;
150+ } )
151+ . catch ( ( err ) => {
152+ reject ( err ) ;
153+ } ) ;
148154
149155 } ) ;
150156 return p ;
@@ -166,7 +172,7 @@ module.exports = function (RED) {
166172 var node = this ;
167173 RED . nodes . createNode ( this , config ) ;
168174
169- this . on ( 'input' , function ( msg ) {
175+ this . on ( 'input' , function ( msg , send , done ) {
170176 var message = '' ,
171177 fileInfo = '' ,
172178 fileSuffix = '' ,
@@ -177,7 +183,7 @@ module.exports = function (RED) {
177183 apikey = sApikey || this . credentials . apikey ;
178184
179185 endpoint = sEndpoint ;
180- if ( ( ! config [ 'default-endpoint' ] ) && config [ 'service-endpoint' ] ) {
186+ if ( config [ 'service-endpoint' ] ) {
181187 endpoint = config [ 'service-endpoint' ] ;
182188 }
183189
@@ -223,12 +229,13 @@ module.exports = function (RED) {
223229 . then ( function ( ) {
224230 temp . cleanup ( ) ;
225231 node . status ( { } ) ;
226- node . send ( msg ) ;
232+ send ( msg ) ;
233+ done ( ) ;
227234 } )
228235 . catch ( function ( err ) {
229236 temp . cleanup ( ) ;
230- payloadutils . reportError ( node , msg , err ) ;
231- node . send ( msg ) ;
237+ let errMsg = payloadutils . reportError ( node , msg , err ) ;
238+ done ( errMsg ) ;
232239 } ) ;
233240 } ) ;
234241 }
0 commit comments