javascript - 如何使用node.js 访问cloudant 文档中的数据?

标签 javascript node.js ibm-cloud cloudant

我在bluemix的index.html页面中编写了以下代码:

$(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);
}
  });

我想从cloudant数据库获取文档并将其显示在表格中。

最佳答案

要从 Node.js 访问 Cloudant,您可以使用 Cloudant 的 Node.js API:

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

下面的代码是从 about github 目录中的 README.md 文件复制的。 它连接到 cloudant,设置要使用的“animals”数据库并获取“dogs”文档:

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);
  });
});

另请查看 Bluemix 中提供的 Node.js Cloudant DB Web Starter Boilerplate:

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

它提供了一个连接到 Cloudant 并使用 API 的多种方法的示例应用程序。

关于javascript - 如何使用node.js 访问cloudant 文档中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32828987/

相关文章:

json - 将 Json 转换为 Xlsx 文件

postgresql - IBM Bluemix 容器上的 Kubernetes 上的 Postgres

mysql - Bluemix ClearDB - 表在不活动后不存在

javascript - 在 Kendo UI Mobile kendoMobileListView 中,选择列表中的项目后清除可过滤输入字段

javascript - 在javascript中调用内部函数

javascript - d3.nest 具有可变子深度

python - 如何配置 elasticsearch 以使用具有基本身份验证的 SSL

javascript - Node 调度仅运行一次

node.js - Web3 + 坚固性 : Passing in arguments to a contract's constructor

javascript - 使用 NodeJS 从 AWS S3 流式传输文件