使用nodejs nano访问Cloudant

时间:2014-09-09 01:07:32

标签: node.js cloudant

我是否有一个要点,我可以使用nano / nodejs连接到cloudant。或者,是否有任何其他库可用于从nodejs连接到cloudant。这是我遇到的错误,当我尝试连接时。

  description: 'one of _writer, _creator is required for this request',

以下是我的代码的外观

// require nano, point it at cloudant's root
var config = require('config');
var nano = require('nano')({url: config.cloudant.url})
    , username = config.cloudant.username
    , userpass = config.cloudant.password
    , cookies = {}
    , callback = console.log // this would normally be some callback

  ;


nano.auth(username, userpass, function (err, body, headers) {
    if (err) {
        return callback(err);
    }

    if (headers && headers['set-cookie']) {
        cookies['user'] = headers['set-cookie'];
    }

    callback(null, "it worked");
});




var db = require('nano')({
    'url': config.cloudant.url + '/' + config.cloudant.database,
    'cookie': cookies['user']
});

在此之后,当我尝试连接时 - 我收到错误

var db = require('nano')({
    'url': config.cloudant.url + '/' + config.cloudant.database,
    'cookie': cookies['user']
});

var doc = {"hello": "world"};

db.insert(doc, function (err, body, headers) {
    if (err) {
        return callback(err);
    }

    // change the cookie if couchdb tells us to
    if (headers && headers['set-cookie']) {
        cookies['user'] = headers['set-cookie'];
    }

    callback(null, "it worked");
});

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此https://github.com/cloudant/nodejs-cloudant。它是nano的包装器,可直接与cloudant连接。