Roth Roth - 4 years ago 137
Javascript Question

.find is not a function mongoose + nodejs + express

I am trying to do a find with mongoose, but I get this


"TypeError: Query.find is not a function"


I have this model:

// file: ./models/request.js

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var dnSchema = new Schema({
customerId: String,
uuid: String,
get_data: String,
get_scores: String
});

dnSchema.index({ customerId: 1, time: -1 });

module.exports = mongoose.model('dN', dnSchema);


And I have this controller

var mongoose = require('mongoose');
var dn = mongoose.model('dn');

(...)
var getScores = exports.getScores = function(req, res) {
var Query = new dn();
console.log(Query)
Query.find({}, function(err, example) {
res.status(200).send(example)
});
}


And this index.js

var mongoose = require('mongoose');

mongoose.connect(config.url, function(err, res) {
if(err) {
logger.error('Error connecting to Database ' + process.pid);
throw err;
}
});

var models = require('./models/request')(app, mongoose);
var controllers = require('./controller/request');

var router = express.Router();

router.route('/get_scores')
.get(controllers.getScores);

app.use(router);
var httpServer = http.createServer(app);
httpServer.listen(config.port, function (){
controllers.logIn();

});


I am trying to do a simple .find, but I can do it.

I hope your help mates!!

Thanks you!!

Answer Source

Try to import the Schema in your controller and use that one.

var dn = require('path to schema file');

(...)
var getScores = exports.getScores = function(req, res) {
  dn.find({}, function(err, example) {
    res.status(200).send(example)
  });
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download