@@ -240,52 +240,63 @@ module.exports = function(RED) {
240
240
} ;
241
241
242
242
Cloudant ( credentials , function ( err , cloudant ) {
243
- if ( err ) { node . error ( err . description , err ) ; }
243
+ if ( err ) {
244
+ node . error ( err . description , err ) ;
245
+ node . cloudantError = err ;
246
+ }
244
247
else {
245
- node . on ( "input" , function ( msg ) {
246
- var db = cloudant . use ( node . database ) ;
247
- var options = ( typeof msg . payload === "object" ) ? msg . payload : { } ;
248
-
249
- if ( node . search === "_id_" ) {
250
- var id = getDocumentId ( msg . payload ) ;
251
- var attachmentName = getAttachementName ( msg . payload ) ;
252
- var attachmentType = getAttachementType ( msg . payload ) ;
253
- node . inputId = id ;
254
- if ( attachmentName ) {
255
- if ( attachmentType ) {
256
- db . attachment . get ( id , attachmentName , function ( err , body ) {
257
- sendAttachementOnPayload ( err , body , msg , attachmentType ) ;
258
- } ) ;
259
- } else {
260
- db . get ( id , function ( err , body ) {
261
- attachmentType = body . _attachments [ attachmentName ] [ "content_type" ] ;
262
- db . attachment . get ( id , attachmentName , function ( err , body ) {
263
- sendAttachementOnPayload ( err , body , msg , attachmentType ) ;
264
- } ) ;
265
- } ) ;
266
- }
267
- } else {
268
- db . get ( id , function ( err , body ) {
269
- sendDocumentOnPayload ( err , body , msg ) ;
270
- } ) ;
271
- }
272
- }
273
- else if ( node . search === "_idx_" ) {
274
- options . query = options . query || options . q || formatSearchQuery ( msg . payload ) ;
275
- options . include_docs = options . include_docs || true ;
276
- options . limit = options . limit || 200 ;
248
+ node . cloudant = cloudant ;
249
+ }
250
+ } ) ;
277
251
278
- db . search ( node . design , node . index , options , function ( err , body ) {
279
- sendDocumentOnPayload ( err , body , msg ) ;
252
+ node . on ( "input" , function ( msg ) {
253
+ if ( ! node . cloudant ) {
254
+ //msg.payload = node.cloudantError;
255
+ msg . payload = null ;
256
+ node . error ( node . cloudantError , msg ) ;
257
+ return ;
258
+ }
259
+ var db = cloudant . use ( node . database ) ;
260
+ var options = ( typeof msg . payload === "object" ) ? msg . payload : { } ;
261
+
262
+ if ( node . search === "_id_" ) {
263
+ var id = getDocumentId ( msg . payload ) ;
264
+ var attachmentName = getAttachementName ( msg . payload ) ;
265
+ var attachmentType = getAttachementType ( msg . payload ) ;
266
+ node . inputId = id ;
267
+ if ( attachmentName ) {
268
+ if ( attachmentType ) {
269
+ db . attachment . get ( id , attachmentName , function ( err , body ) {
270
+ sendAttachementOnPayload ( err , body , msg , attachmentType ) ;
280
271
} ) ;
281
- }
282
- else if ( node . search === "_all_" ) {
283
- options . include_docs = options . include_docs || true ;
284
-
285
- db . list ( options , function ( err , body ) {
286
- sendDocumentOnPayload ( err , body , msg ) ;
272
+ } else {
273
+ db . get ( id , function ( err , body ) {
274
+ attachmentType = body . _attachments [ attachmentName ] [ "content_type" ] ;
275
+ db . attachment . get ( id , attachmentName , function ( err , body ) {
276
+ sendAttachementOnPayload ( err , body , msg , attachmentType ) ;
277
+ } ) ;
287
278
} ) ;
288
279
}
280
+ } else {
281
+ db . get ( id , function ( err , body ) {
282
+ sendDocumentOnPayload ( err , body , msg ) ;
283
+ } ) ;
284
+ }
285
+ }
286
+ else if ( node . search === "_idx_" ) {
287
+ options . query = options . query || options . q || formatSearchQuery ( msg . payload ) ;
288
+ options . include_docs = options . include_docs || true ;
289
+ options . limit = options . limit || 200 ;
290
+
291
+ db . search ( node . design , node . index , options , function ( err , body ) {
292
+ sendDocumentOnPayload ( err , body , msg ) ;
293
+ } ) ;
294
+ }
295
+ else if ( node . search === "_all_" ) {
296
+ options . include_docs = options . include_docs || true ;
297
+
298
+ db . list ( options , function ( err , body ) {
299
+ sendDocumentOnPayload ( err , body , msg ) ;
289
300
} ) ;
290
301
}
291
302
} ) ;
0 commit comments