diff --git a/77-cloudant-cf.js b/77-cloudant-cf.js index ff1d580..c22fb13 100644 --- a/77-cloudant-cf.js +++ b/77-cloudant-cf.js @@ -81,6 +81,7 @@ module.exports = function(RED) { this.operation = n.operation; this.payonly = n.payonly || false; this.database = _cleanDatabaseName(n.database, this); + console.log("CloudantOutNode this.database = " + this.database); this.cloudantConfig = _getCloudantConfig(n); var node = this; @@ -226,12 +227,14 @@ module.exports = function(RED) { this.cloudantConfig = _getCloudantConfig(n); this.database = _cleanDatabaseName(n.database, this); + console.log("CloudantInNode this.database = " + this.database); this.search = n.search; this.design = n.design; this.index = n.index; this.inputId = ""; var node = this; + node.log("cloudantConfig: " + JSON.stringify(node.cloudantConfig)); var credentials = { account: node.cloudantConfig.account, key: node.cloudantConfig.username, @@ -240,36 +243,47 @@ module.exports = function(RED) { }; Cloudant(credentials, function(err, cloudant) { - if (err) { node.error(err.description, err); } + if (err) { + node.DBError = err; + node.error(err.description, err); + } else { - node.on("input", function(msg) { - var db = cloudant.use(node.database); - var options = (typeof msg.payload === "object") ? msg.payload : {}; + node.cloudant = cloudant; + } + }); + node.on("input", function(msg) { + if (!node.cloudant){ + msg.payload = node.DBError; + node.error(node.DBError, msg); + return; + } + //var db = node.cloudant.use(node.database); + var dbName = msg.payload.dbName || node.database; + var db = node.cloudant.use(dbName); + var options = (typeof msg.payload === "object") ? msg.payload : {}; - if (node.search === "_id_") { - var id = getDocumentId(msg.payload); - node.inputId = id; + if (node.search === "_id_") { + var id = getDocumentId(msg.payload); + node.inputId = id; - 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.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.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); - }); - } + db.list(options, function(err, body) { + sendDocumentOnPayload(err, body, msg); }); } }); @@ -340,9 +354,11 @@ module.exports = function(RED) { // password for the Cloudant service at the top-level of the object function _getCloudantConfig(n) { if (n.service === "_ext_") { + console.log("get _ext_ configuration of " + n.cloudant); return RED.nodes.getNode(n.cloudant); } else if (n.service !== "") { + console.log("get cloud configuration of " + n.cloudant); var service = appEnv.getService(n.service); var cloudantConfig = { };