Skip to content

Commit

Permalink
Fix for: No response on wrong Cloudant credentials.
Browse files Browse the repository at this point in the history
  • Loading branch information
Orit Prince committed Oct 9, 2016
1 parent f2f3a7d commit d36de1b
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions 77-cloudant-cf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
Expand Down

1 comment on commit d36de1b

@oprince
Copy link

@oprince oprince commented on d36de1b Oct 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When wrong credentials are set for Cloudant In node, the error is reported only during node initiation.
Later, when flow is executed, no error is thrown or reported, and flow hangs because it has no definition for callback function of node.on("input",).
I have fixed it so node.on("input",) is defined always, even when cloudant connection does not success.

Please sign in to comment.