Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I keep getting "db.collection is not a function" in the browser This is the MEAN stack

I have node.js, mongo, express, npm, nodemon, mocha, body-parser, and npm mongodb installed and working the first file is my server file called app.js

const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const routes = require('./api/routes');
require('./api/data/dbconnection.js').open();


app.set('port', 3000);

let server = app.listen(app.get('port'), function() {
    let port = server.address().port;
    console.log('Port ' + port);
});

app.use(express.static(path.join(__dirname, 'public')));

app.use(bodyParser.urlencoded({ extended : false }));

app.use('/api', routes);

the next is my routes file called index.js

let express = require('express');
let routes = express.Router();

let ctrl = require('../controllers/controllers.js');

routes
    .route('/json')
    .get(ctrl.GetAll);

routes
    .route('/json/:ProID')
    .get(ctrl.GetOne);

routes
    .route('/json/new')
    .post(ctrl.profileAddOne);

module.exports = routes;

then controllers.js

let dbconn = require('../data/dbconnection.js');
let profileData = require('../data/proData.json');

module.exports.GetAll = function(req, res) {
   let db = dbconn.get();
   let collect = db.collection('profile');

   collect
    .find()
    .toArray(function(err, docs) {
        res
            .status(200)
            .json(docs);
    });
};
module.exports.GetOne = function(req, res) {
    let ProID = req.
    console.log("GET the json");
    res
        .status(200)
        .json( ProID );
};
module.exports.profileAddOne = function(req, res) {
    res
        .status(200)
        .json(req.body);
}

Then my dbconnection.js

let MongoClient = require('mongodb').MongoClient;
let dburl = 'mongodb://localhost:27017/profileDB';

let _connection = null;

let open = function() {
    MongoClient.connect(dburl, function(err, db) {
        if (err) {
            console.log("DB connection Error");
            return;
        }
        _connection = db;
        console.log("DB connection open", db);
    });
};

let get = function() {
    return _connection;
};

module.exports = {
    open : open,
    get : get
};

any help would be awesome thank you.

4 Answers

Steven Parker
Steven Parker
230,024 Points

You didn't provide a course link, but just a wild guess: instead of "collection", might you have meant "connection"?

STEVEN!!!! how in the world are you? this is actually a home project but I did try connection, and no dice:(

Steven Parker
Steven Parker
230,024 Points

I'm pretty good! And if you can put this into a workspace, I can take another look.

How do you put into a workspace?

Steven Parker
Steven Parker
230,024 Points

You can either use the upload function, or create new files and paste in.

hey man sorry for the late response I am currently working two jobs, well three if you count the army reserves and school so my time for my project is limited. I tried to do a work space but my account is inactive right now, so it wont let me put it up there.