Search code examples
node.jsexpressswagger-2.0

Express TypeError object is not a function


I keep getting this error saying TypeError: getVowels.find is not a function. This is a swagger Express project learning how to create a mongoose function to find. What am I doing wrong? Thanks in advance.

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');

var VowelsSchema = require('../models/vowels');

// connect to our database
mongoose.createConnection('mongodb://127.0.0.1:27017');

module.exports = router;

router.get('/', function(req, res) {
  var getVowels = new VowelsSchema();      

  getVowels.find(function(err, vowels) {
    if (err) {
      res.send(err);
    }
    res.json(vowels);
  });
});

Solution

  • You are wrongly using find method. It doesn't work on instance of your model, but it works on the model itself.

    So instead of getVowel.find(function(err,vowels){...}) , try this

    VowelsSchema.find({},function(err,vowels){//its always a good practice to ue {} in find
        if (err) {
          res.send(err);
        }
        res.json(vowels);
    });