M
M
Michael2015-11-27 10:59:00
MongoDB
Michael, 2015-11-27 10:59:00

Error while working with MongoDB?

Hello!
MongoDB version 3.0.7
I just started learning MongoDB and immediately ran into a problem.
There is this simple code:

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/mongotest',
  function(err, db) {
    console.log('Connected to MongoDB!');
    // using the db connection object, save the collection 'testing' to
    // a separate variable:
    var collection = db.collection('testing');
    // isert a new item using the collection's insert function:
    collection.insert({
      'title': 'Snowcrash'
    }, function(err, docs) {
      console.log(docs.length + ' record inserted.');
      console.log(docs[0].title + ' – ' + docs[0]._id);
      
      db.close();
    });
  });

And there is such an error
d3ecd1a786b34d92ab040da9ce3cc2ba.png
Help me understand what kind of error it is, and how to fix it, if not difficult.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-11-27
@alexey-m-ukolov

  1. The console prints "undefined record inserted".
  2. So docs.length is undefined.
  3. So the insert failed.
  4. So, you need to look at the contents of the error, which is passed as the first parameter to the function for precisely these purposes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question