A
A
Alexander Sharomet2016-07-20 15:02:10
JavaScript
Alexander Sharomet, 2016-07-20 15:02:10

How to get data from a collection?

Hi everyone
, I'm facing the same problem. I can't display data from mongodb.
Everything works in the console, but when I connect, I can only add data, but I can’t display it, it writes an error.

TypeError: Cannot read property 'find' of undefined
    at \NodeTemplate\server.js:28:25
    at \NodeTemplate\node_modules\mongodb\lib\mongo_client.js:210:11
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

The script itself
MongoClient.connect("mongodb://localhost:27017/nodedb", function(err, db) {
    if(!err) {
        console.log("We are connected");
    }
    console.log(db.users.find({"name":"Alex"}));
});

What could be the problem? Thank you.
MongoClient.connect("mongodb://localhost:27017/nodedb", function(err, db) {
    if(!err) {
        console.log("We are connected");
    }
    var user = {_id:2,name:"Alex",password:"123456"};
    collection.insert(user);
});

//'этот скрипт работает. он добавляет данные в колекцию

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2016-07-20
@sharomet

console.log(collection.find({"name":"Alex"}));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question