From d36de1bf9de527114bd22a492d8fe0f20b931fae Mon Sep 17 00:00:00 2001 From: Orit Prince Date: Sun, 9 Oct 2016 08:45:21 +0300 Subject: [PATCH] Fix for: No response on wrong Cloudant credentials. --- 77-cloudant-cf.js | 93 ++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/77-cloudant-cf.js b/77-cloudant-cf.js index fbb8b6e..8fe02d8 100644 --- a/77-cloudant-cf.js +++ b/77-cloudant-cf.js @@ -240,52 +240,63 @@ module.exports = function(RED) { }; Cloudant(credentials, function(err, cloudant) { - if (err) { node.error(err.description, err); } + if (err) { + node.error(err.description, err); + node.cloudantError = err; + } else { - node.on("input", function(msg) { - var db = cloudant.use(node.database); - var options = (typeof msg.payload === "object") ? msg.payload : {}; - - if (node.search === "_id_") { - var id = getDocumentId(msg.payload); - var attachmentName = getAttachementName(msg.payload); - var attachmentType = getAttachementType(msg.payload); - node.inputId = id; - if (attachmentName){ - if (attachmentType){ - db.attachment.get(id, attachmentName, function(err, body) { - sendAttachementOnPayload(err, body, msg, attachmentType); - }); - }else{ - db.get(id, function(err, body) { - attachmentType = body._attachments[attachmentName]["content_type"]; - db.attachment.get(id, attachmentName, function(err, body) { - sendAttachementOnPayload(err, body, msg, attachmentType); - }); - }); - } - }else{ - db.get(id, function(err, body) { - sendDocumentOnPayload(err, body, msg); - }); - } - } - else if (node.search === "_idx_") { - options.query = options.query || options.q || formatSearchQuery(msg.payload); - options.include_docs = options.include_docs || true; - options.limit = options.limit || 200; + node.cloudant = cloudant; + } + }); - db.search(node.design, node.index, options, function(err, body) { - sendDocumentOnPayload(err, body, msg); + node.on("input", function(msg) { + if (!node.cloudant){ + //msg.payload = node.cloudantError; + msg.payload = null; + node.error(node.cloudantError, msg); + return; + } + var db = cloudant.use(node.database); + var options = (typeof msg.payload === "object") ? msg.payload : {}; + + if (node.search === "_id_") { + var id = getDocumentId(msg.payload); + var attachmentName = getAttachementName(msg.payload); + var attachmentType = getAttachementType(msg.payload); + node.inputId = id; + if (attachmentName){ + if (attachmentType){ + db.attachment.get(id, attachmentName, function(err, body) { + sendAttachementOnPayload(err, body, msg, attachmentType); }); - } - else if (node.search === "_all_") { - options.include_docs = options.include_docs || true; - - db.list(options, function(err, body) { - sendDocumentOnPayload(err, body, msg); + }else{ + db.get(id, function(err, body) { + attachmentType = body._attachments[attachmentName]["content_type"]; + db.attachment.get(id, attachmentName, function(err, body) { + sendAttachementOnPayload(err, body, msg, attachmentType); + }); }); } + }else{ + db.get(id, function(err, body) { + sendDocumentOnPayload(err, body, msg); + }); + } + } + else if (node.search === "_idx_") { + options.query = options.query || options.q || formatSearchQuery(msg.payload); + options.include_docs = options.include_docs || true; + options.limit = options.limit || 200; + + db.search(node.design, node.index, options, function(err, body) { + sendDocumentOnPayload(err, body, msg); + }); + } + else if (node.search === "_all_") { + options.include_docs = options.include_docs || true; + + db.list(options, function(err, body) { + sendDocumentOnPayload(err, body, msg); }); } });