A
A
artem201520002015-02-11 21:20:43
MongoDB
artem20152000, 2015-02-11 21:20:43

Why can't use collection method in node.js when working with mongodb?

So I wrote a node script that connects to the mongodb database. But when the collection and records are added, an error pops up with 'has no method collecton'. This is very strange because there are a lot of examples on the Internet with the collection method!!
Maybe, of course, I mixed something up, so here is the error itself:
TypeError: Object function MongoClient(serverConfig, options) {
if(serverConfig != null) {
options = options ? options : {};
// If no write concern is set set the default to w:1
if('w' in options === false) {
options.w = 1;
}
// The internal db instance we are wrapping
this._db = new Db('test', serverConfig, options);
}
} has no method 'collection'
at D:\programms\website\app.js:26:31
at Layer.handle [as handle_request] (D:\programms\website\node_modules\express\lib\router\layer.js: 82:5)
at next (D:\programms\website\node_modules\express\lib\router\route.js:100:13)
at Route.dispatch (D:\programms\website\node_modules\express\lib\router\ route.js:81:3)
at Layer.handle [as handle_request] (D:\programms\website\node_modules\express\lib\router\layer.js:82:5)
at D:\programms\website\node_modules\ express\lib\router\index.js:234:24
at Function.proto.process_params (D:\programms\website\node_modules\express\lib\router\index.js:312:12)
at D:\programms\website \node_modules\express\lib\router\index.js:228:12
at Function.match_layer (D:\programms\website\node_modules\express\lib\router\index.js:295:3)
at next (D:\programms\website\node_modules\express\lib\router\index.js: 189:10)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kalibrov, 2015-02-12
@rework

So apparently collection is not a method, but the name of the collection, substitute the name of your collection instead.
Give an example of your code, please, and not just an error.

P
pomeo, 2015-02-12
@pomeo

You have an error in the line var collection = MongoClient.Collection('users');
MongoClient does not have a collection method, only connect, open, close and db mongodb.github.io/node-mongodb-native/api-generate...
And you need to do the following, see an example here mongodb.github.io/node-mongodb -native/driver-artic...
i.e. your option would be var collection = db.collection('users');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question