mandy Yiu
mandy Yiu

Reputation: 81

Authentication failed. OpenBucket in couchbase

My task is set up a couchbase server using node.js with two endpoints. When i would like connecting a bucket I got the error shown as follows:

CouchbaseError: Authentication failed. You may have provided an invalid username/password combination message: 'Authentication failed. You may have provided an invalid username/password combination', code: 2

And my code app.js is as follows

var express = require("express");
var couchbase = require("couchbase");
var bodyParser = require("body-parser");

var cluster = new couchbase.Cluster('couchbase://localhost');
var bucket = cluster.openBucket('example'); //the name of bucket is 'example'
bucket.on('error', function(err) {
  console.log('Bucket: CONNECT ERROR:', err);});

var app=express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));

// create two endpoints 

app.get("/person/:id", function(req, res){
  bucket.get(req.params.id, function (error, result){
     if(error){
         console.log("error in get method");
         return res.status(400).send(error);
     }
     res.send(result); 
 });
});

app.post("/person/:id", function(req, res){
  var document = {
    firstName :req.body.firstName,
    lastName : req.body.lastName
  }
  bucket.upsert(req.params.id, function (error, result){
    if(error){
        console.log("error in post method");
        return res.status(400).send(error);
    }
    res.send(result); 
 });
});

var server = app.listen(3000, function(){
  console.log("Listening on port %s...", server.address().port); 
});

Upvotes: 2

Views: 1265

Answers (2)

KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20098

We need to update authenticate details, then it work fine.

cluster.authenticate({username:"admin", password: "password"});

Upvotes: 0

Ramitha Rupasinghe
Ramitha Rupasinghe

Reputation: 203

You need give the couchbase login credentials (cluster.authenticate) when you are making the database connection.

    var cluster = new couchbase.Cluster('couchbase://localhost');
    cluster.authenticate('username', 'password'); 
    var bucket = cluster.openBucket('database-name'); //the name of bucket
    bucket.on('error', function(err) {
      console.log('Bucket: CONNECT ERROR:', err);});

Upvotes: 5

Related Questions