简体   繁体   中英

.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!!

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)
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM