Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

For this controller I am simply trying to return all the results of a collection. There is one item in the prop collection but for some reason I keep getting an undefined error.

TypeError: Cannot call method 'find' of undefined

This is my server.js file:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
passport = require('passport'),
logger = require('mean-logger');

   /**
 * Main application entry file.
 * Please note that the order of loading is important.
*/

// Initializing system variables
var config = require('./server/config/config');
var db = mongoose.connect(config.db);

// Bootstrap Models, Dependencies, Routes and the app as an express app
var app = require('./server/config/system/bootstrap')(passport, db);

// Return all results.
app.get('/all', function(req, res) {

  var d = db.prop.find();

  res.json(d);

});

// Start the app by listening on <port>, optional hostname
app.listen(config.port, config.hostname);

// Initializing logger
logger.init(app, passport, mongoose);

// Expose app
exports = module.exports = app;
share|improve this question
    
If you are using passport/mongo - do you have any virtual schemas for mongoose...usually you do something like User.find({}) to get all values - actually...maybe what u are doing is ok - but u might need db.prop.find({}); – ewizard Jun 17 '14 at 23:21
    
add the brackets inside the parentheses. – ewizard Jun 17 '14 at 23:21
    
with mongoose the empty brackets signify "all" not "none" - a little counter-intuitive – ewizard Jun 17 '14 at 23:23
1  
While you don't need the empty braces, @ewizard is correct that you need to call find on your correct Mongoose model; so something like Prop.find(). – JohnnyHK Jun 18 '14 at 0:00
    
thanks guys, the issue is the collection is undefined for some reason. db is defined but db.prop is undefined. But in terminal in the mongo shell I can view the table and see the data. – user1572796 Jun 18 '14 at 18:23

Im going about this all wrong, Im used to js with restful controllers. Setting up the controller route in server.js is correct but making mongodb calls is actually done in the model level in the mean stack so this not working is as expected functionality. Basically you dont need ajax to query a mongo db. Just not used to programming this way, thanks for feedback though.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.