0

I wrote the the below code in the index.html page of bluemix:

$(document).ready(function() {
                $("button").click(function() {
                var Cloudant = require('cloudant');
                var password = "#password";
                var myAccount = "#accountname";
                Cloudant({account:myAccount, password:password}, /* @callback */ function(err, cloudant,body) {
  if (err) {
    return ("Failed");
  } else {
                printTable(body);
}
  });

I want to get a document from cloudant database and display it in a table.

1 Answer 1

1

To access Cloudant from Node.js you can use the Node.js API for Cloudant:

https://github.com/cloudant/nodejs-cloudant

The code below was copied from the README.md file from the about github directory. It connects to cloudant, set the "animals" db for use and get the "dogs" documents:

var Cloudant = require('cloudant');
var me = 'nodejs'; // Replace with your account.
var password = process.env.cloudant_password;

Cloudant({account:me, password:password}, function(err, cloudant) {
  if (err) {
    return console.log('Failed to initialize Cloudant: ' + err.message);
  }

  var db = cloudant.db.use("animals");
  db.get("dog", function(err, data) {
    // The rest of your code goes here. For example:
    console.log("Found dog:", data);
  });
});

Also take a look at the Node.js Cloudant DB Web Starter Boilerplate available in Bluemix:

https://console.ng.bluemix.net/catalog/nodejs-cloudant-db-web-starter/

It provides a sample application that connects to Cloudant and uses several methods of the API.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alex for the answer. When I write this piece of code, I get a javascript error saying that "require is not defined". Can you please help me avoid this error.
Suchitra, this is just a piece of code, not the complete code. If you create the Node.js Cloudant DB Web Starter application in Bluemix and download the code, the app.js will have a more complete example. Also note this is Node.js server side code, not pure JavaScript client code (i.e not meant to be used in the index.html page you mentioned initially.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.