1

New to node and mongodb I am working on a linux Centos 7 enviorment. For some reason I get this error 'TypeError: db.model is not a function'

What does this error means?

    /var/www/html/mongo/crud/src/model/task.js:11
    return db.model('tasks',Task);
              ^

TypeError: db.model is not a function
    at module.exports (/var/www/html/mongo/crud/src/model/task.js:11:15)
    at Object.<anonymous> (/var/www/html/mongo/crud/src/routes/index.js:4:39)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/html/mongo/crud/src/app.js:9:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
[nodemon] app crashed - waiting for file changes before starting...

this is the content of task.js file:

module.exports = function(){

    var db = require('../libs/db-connection')();
    var Schema = require('mongoose').Schema;

    var Task = Schema({
        title: String,
        description: String,
        status: Boolean
    });
    return db.model('tasks',Task);
}

This the content of index.js file:

const express = require('express');
const router = express.Router();

const model = require('../model/task')();

router.get('/' ,(req, res) => {
    model.find({}, (err, tasks) =>{
        if (err) throw err;
        res.render('index', {
            title: 'CRUD',
            task: tasks
        });
    });
});

module.exports = router;
3
  • 1
    whatever require('../libs/db-connection')() returns, it has no function model. Commented Feb 1, 2018 at 14:09
  • You want to use model function from mongoose?? Commented Feb 1, 2018 at 14:19
  • Yes, I want to use model from mongoose Commented Feb 1, 2018 at 14:35

1 Answer 1

2

Unless you have a model function in your libs/db-connection, I think that you want to use the model from mongoose.

If this is right, to fix your problem you need to change the import from mongoose, like this:

var mongoose = require('mongoose');  // Not require('mongoose').Schema (because you need schema and model)

var Task = mongoose.Schema({  // Call Schema from mongoose
    title: String,
    description: String,
    status: Boolean
});

return mongoose.model('tasks',Task);  // Call model from mongoose
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, The error is gone but I receive 'The page isn't working error' What can be the reason of it? this is how the task file looks like. is it still sopouse to be inside the function? what about the connection to the database? where do I place it? module.exports = function(){ var mongoose = require('mongoose'); var Task = mongoose.Schema({ title: String, description: String, status: Boolean }); return mongoose.model('tasks',Task); }
That looks like another error, my recomendation is to accept my answer and post another question, with full description of the error and the updated/relevant code, so myself and other can help you with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.