user2325703
user2325703

Reputation: 1241

Error while inserting file to MongoDB from Node.js

I am trying to connect to mongoDB from node.js and upload a file("functions") to MongoDB. Can someone please verify whats the issue with my code is.

When I run the js file, I am getting following error: Error: Cannot find module 'mongodb' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25)

Code is as follows:

  var mongodb = require('mongodb');
   var url = require('url');
   var log = console.log;
   var currentTimeStamp = new Date();
   var file = require (__dirname + '/functions');

mongodb.MongoClient.connect('mongodb://phx8b03c-fb1d-6.stratus.phx.ebay.com,phx8b03c-316d-6.stratus.phx.ebay.com,phx8b03c-9564-6.stratus.phx.ebay.com',
    function (err, client) {
        if (err) throw err;

        client.createCollection('lbTopology' , function (err, collection) {
            if (err) throw err;

            collection.insert(file, 'lbTopology' , function (err) {
                if (err) throw err;

                client.close(function (err) {
                    if (err) throw err;

                    console.log('done');
                });
            });
        });
    }); 

Can someone please let me know what the issue is? Thanks a lot in advance

Upvotes: 2

Views: 293

Answers (1)

sheldonk
sheldonk

Reputation: 2694

It looks like you don't have mongodb installed. Did you npm install mongodb in the same directory with your code or do you have a node_modules folder with mongodb in it?

Upvotes: 2

Related Questions