Module.exports

Im trying to export data into the app.js file here is what I have so far

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/test";


// index page 
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("customers").find({}).toArray(function(err, result) {
  if (err) throw err;
  db.close();
});

});

How can I edit this so I can import this into my app.js file ?

I have tried something like this but I have something wrong.

var MongoClient = require(‘mongodb’).MongoClient;
var url = “mongodb://127.0.0.1:27017/test”;

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("customers").find({}).toArray(function(err, result) {
  if (err) throw err;
  db.close();
});

});

module.exports={
result

}

Console.log tells me results is not defined

any tips would be much appreciated

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.