I am trying to get data from mongoDB while connecting to server .i inserted one value in mongoDB like this
> use abc
switched to db abc
> db.ac.insert({name:"naveen"})
WriteResult({ "nInserted" : 1 })
> show collections
ac
system.indexes
And try to get that value like this
var express=require('express');
var app =express();
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
app.get('/',function(req,res){
console.log("Connected to server.");
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var collection = db.collection('ac');
console.log(collection)
console.log("Connected correctly to server.");
db.close();
});
})
var url = 'mongodb://localhost:27017/abc';
app.listen(3000,function(){
console.log('server is runninngn')
})
I am getting this error why ?
MacBook-Pro:expressjs naveenkumar$ node index.js
server is runninngn
Connected to server.
{ s:
{ pkFactory:
{ [Function: ObjectID]
index: 462263,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString],
isValid: [Function: isValid],
ObjectID: [Circular],
ObjectId: [Circular] },
db:
{ domain: null,
_events: {},
_maxListeners: 10,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
options: [Getter],
native_parser: [Getter],
slaveOk: [Getter],
writeConcern: [Getter] },
topology:
{ domain: null,
_events: [Object],
_maxListeners: 10,
s: [Object],
bson: [Getter],
isMasterDoc: [Getter],
poolSize: [Getter],
autoReconnect: [Getter],
host: [Getter],
port: [Getter],
emitOpen: false,
connectTimeoutMS: 30000,
socketTimeoutMS: 0 },
dbName: 'abc',
options: { promiseLibrary: [Object], readConcern: undefined },
namespace: 'abc.ac',
readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
raw: undefined,
slaveOk: true,
serializeFunctions: undefined,
internalHint: null,
collectionHint: null,
name: 'ac',
promiseLibrary:
{ [Function: lib$es6$promise$promise$$Promise]
all: [Function: lib$es6$promise$promise$all$$all],
race: [Function: lib$es6$promise$promise$race$$race],
resolve: [Function: lib$es6$promise$promise$resolve$$resolve],
reject: [Function: lib$es6$promise$promise$reject$$reject] },
readConcern: undefined } }
Connected correctly to server.
Connected to server.
{ s:
{ pkFactory:
{ [Function: ObjectID]
index: 462263,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString],
isValid: [Function: isValid],
ObjectID: [Circular],
ObjectId: [Circular] },
db:
{ domain: null,
_events: {},
_maxListeners: 10,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
options: [Getter],
native_parser: [Getter],
slaveOk: [Getter],
writeConcern: [Getter] },
topology:
{ domain: null,
_events: [Object],
_maxListeners: 10,
s: [Object],
bson: [Getter],
isMasterDoc: [Getter],
poolSize: [Getter],
autoReconnect: [Getter],
host: [Getter],
port: [Getter],
emitOpen: false,
connectTimeoutMS: 30000,
socketTimeoutMS: 0 },
dbName: 'abc',
options: { promiseLibrary: [Object], readConcern: undefined },
namespace: 'abc.ac',
readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
raw: undefined,
slaveOk: true,
serializeFunctions: undefined,
internalHint: null,
collectionHint: null,
name: 'ac',
promiseLibrary:
{ [Function: lib$es6$promise$promise$$Promise]
all: [Function: lib$es6$promise$promise$all$$all],
race: [Function: lib$es6$promise$promise$race$$race],
resolve: [Function: lib$es6$promise$promise$resolve$$resolve],
reject: [Function: lib$es6$promise$promise$reject$$reject] },
readConcern: undefined } }
**Connected correctly to server.**
Please why it is giving error
See Question&Answers more detail:
os