我在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/