I'm new with Couchbase and trying to use the couchbase module 1.2.1, the connection to the DB seems to work fine, but the get and getMulti operations are failing and the error I'm getting is "Operation timed out".
I tried to increase the timeout, but it didn't help.
var db = new couchbase.Connection({ host:'localhost:8091', bucket:'beer-sample'},
function(err){
if (err){
throw err; // not getting here
}
});
db.get("id", function(err, result) {
if (!err && result){ // getting error
req.id = result;
}
});
What can be the problem?
You should try to reinstall Couchbase Server and try again using a host of 127.0.0.1:8091.
What version of Couchbase Server, and what platform/architecture you are using would also be helpful to know.
Related
I am attempting to create a mongo instance however I am unable to access any of the helper methods from the mongodb nodejs driver.
My mongo instance is running within docker and the ports have been opened up to my local.
TypeError: db.createCollection is not a function
at /var/www/html/beacon/index.js:6:8
at args.push (/var/www/html/beacon/node_modules/mongodb/lib/utils.js:431:72)
at /var/www/html/beacon/node_modules/mongodb/lib/mongo_client.js:254:5
at connectCallback (/var/www/html/beacon/node_modules/mongodb/lib/mongo_client.js:933:5)
at /var/www/html/beacon/node_modules/mongodb/lib/mongo_client.js:794:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
Copied from w3schools...
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
No error is returned through the run, and no methods are exposed on the db object.
any ideas?
According to the changelog for Mongodb 3.0 you now get a client object containing the database object instead:
So you need the db object that points to the database you want to use, in your case mydb. Try this:
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) { //here db is the client obj
if (err) throw err;
var dbase = db.db("mydb"); //here
dbase.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close(); //close method has also been moved to client obj
});
});
You're not the one facing this issue. Seems that 3.0 mongo driver has a bug or these are just breaking backwards compatibility changes. Take a look here:
db.collection is not a function when using MongoClient v3.0
To use DB name in the URL, you need to uninstall MongoDB, change to "mongodb": "^2.2.33" in dependencies and do npm install to install the new version.
Or you can install specific version with command npm install mongodb#2.2.33 --save
I was getting the error while running createCollection() from the command line Mongo shell tool mongosh.
TypeError: db.createCollection is not a function
In my case, I could not get create collection to work, so I resorted to just inserting a document into the collection which I wanted to create. By default, Mongo will create a collection by that name on the fly, assuming you have the permissions to do so. This is not an ideal solution, but it allowed me to proceed at least.
I don't have much knowledge about mongo and NodeJS, and also about server set up so I might be asking something basic.
I'm trying to install MongoDB and NodeJS for school project.
In order to do so I registered to Amazon EC2 server and implemented an instance according to some tutorials I've found on line.
Using MongoDB wiki, I've installed Mongo on the server and using another Tutorial I installed NodeJS.
Just to be super clear, please see below picture who's showing the directory and MongoDB server listening:
http://s23.postimg.org/k02f747m2/mongo_Dir.jpg
After I've done that I'm trying to start using the server, I've created a simple file using the following code:
var mongodb = require('mongodb');
var server = new mongodb.Server('127.0.0.1', 27017, {});
var client = new mongodb.Db('exampledb', server, {w: 1});
client.open(function(err) {
if (err) throw err;
client.collection('students', function(err, collection) {
if (err) throw err;
collection.insert(
{
"name": "Noam",
"year": "2012."
},
{safe: true},
function(err, documents) {
if (err) throw err;
console.log('Document ID is: ' + documents[0]._id);
}
);
});
});
Now I tried to run the file and received the following error
http://s30.postimg.org/sqplfazlt/eror.jpg
Any help?
I am using node v.0.10.33 couchbase, node module v.2.0.0 and couchbase-server-v.3.0.1
var couchbase = require("couchbase");
// Connect to Couchbase Server
var cluster = new couchbase.Cluster('10.50.10.31:8091');
var bucket = cluster.openBucket('beer-sample', function(err) {
if (err) {
// Failed to make a connection to the Couchbase cluster.
throw err;
}
// Retrieve a document
bucket.get('aass_brewery-juleol', function(err, result) {
if (err) {
// Failed to retrieve key
throw err;
}
var doc = result.value;
console.log(doc.name + ', ABV: ' + doc.abv);
// Store a document
doc.comment = "Random beer from Norway";
bucket.replace('aass_brewery-juleol', doc, function(err, result) {
if (err) {
// Failed to replace key
throw err;
}
console.log(result);
// Success!
process.exit(0);
});
});
});
when i run the above program on the same machine in which couchbase server is installed its working fine..
with this line
var cluster = new couchbase.Cluster('127.0.0.1:8091');
But when i run with another system which connected through Local area network I am getting network error. with this line
var cluster = new couchbase.Cluster('10.50.10.31:8091');
this error...
Couchbase Error : Network Failure
also tried
var cluster = new couchbase.Cluster('couchbase://10.50.10.31')
not working...
var cluster = new couchbase.Cluster('couchbase://localhost')
working fine...
Where i am going wrong please help me...
sorry for mistakes.
As per Couchbase Node.js SDK documentation, try creating connection like this:
var couchbase = require("couchbase");
var bucket = new couchbase.Connection({
'bucket':'beer-sample',
'host':'10.50.10.31:8091'
}, function(err) {
if (err) {
// Failed to make a connection to the Couchbase cluster.
throw err;
}
// your code to work with bucket here...
});
The problem is with python and node-gyp
i have upgraded python
and rebuild the couchbase module
cd path_to_nodejs_project/node_modules/coucbase/
node-gyp clean
node-gyp configure
node-gyp build
This solved my problem
I have a one-shot Node script that makes some changes to a MongoDB database on MongoLab. However, once it finishes, it never exits the event loop (I always have to ctrl+C it), no matter how much db.close() and db.logout() calling I do.
What's strange is, if I start a local running instance of mongod and connect to that, the script finishes fine, but the remote connection just never ends.
Here is a short version of my script that still has the issue (taking the URL to the server on the command line). What's going on?
var mongodb = require("mongodb");
function onSuccess(cb){
return function(err) {
if (err) {
console.error(err)
} else {
cb.apply(this,Array.prototype.slice.call(arguments,1))
}
}
}
console.log("Connecting to "+process.argv[2]+' ...');
mongodb.MongoClient.connect(process.argv[2],onSuccess(function(db){
console.log("Connected.");
db.logout(onSuccess(function(logoutResult){
db.close(onSuccess(function(closeResult){
console.log("All finished. Can has prompt return nao?")
}));
}));
}));
Just tried the code with driver version 1.2.7/1.2.8 and the newest 1.2.9 against mongolab and it works correctly. So more likely its a weird combination of driver/os/node version that's causing this. I suggest upgrade your node and driver to the latest version and try again.
I suspect it has to do with the way you have defined your closures but I cannot quite put my finger on it.
For what is worth, below is the approach that I use and this does close the connection as expected:
MongoClient.connect(dbUrl, function(err, db) {
if(err) return callback(err);
var collection = db.collection(dbCollection);
collection.find().toArray(function(err, items){
db.close()
if(err) return callback(err);
callback(null, items);
});
});
You can find a full example here: https://github.com/hectorcorrea/mongoDbSample
I am learning and trying simple example using node.js and mongoskin. here is my function below
Problem following function is, if the mongodb server is disconnected then also I get the "err=null" hence not able catch connection error. If I restart node.js server (while mongoDB server is still disconnected) I get error as
"[Error: failed to connect to [server-aa070:27017]]"
// Process messages from client
app.post('/send', function(req, res){
var message = {
id: i++,
nickname: req.param('nickname', 'Anonymous'),
text: req.param('text', ''),
created_at: new Date()
};
conn.chat_log.insert(message, function(err) {
if(err!==null){
console.log(err);
}
else {
console.log(message);
console.log(err);
}
});
res.json({status: 'ok'});
});
I'm new to node.js and mongodb, but why are you using if(err!==null) rather than if(err)? If I'm understanding correctly, wouldn't this solve your problem?
Don't know about mongoskin, but for the node-mongo-native driver (the driver that mongoskin is built on), the author said:
Note that there's no reason to pass a callback to the insert or update
commands unless you use the safe:true option. If you don't specify
safe:true, then your callback will be called immediately.