I am getting a JS TypeError: undefined is not a function
when trying to create an instance of a schema I define and import.
article.js
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
// Article Schema
var ArticleSchema = new Schema({
title : { type: String, required: true, index: { unique: true }},
author : { type: String, required: true },
day : { type: String, required: true },
month : { type: String, required: true },
year : { type: String, required: true },
link : { type: String, required: true },
password : { type: String, required: false, select: false }
});
module.exports = mongoose.model("Article", ArticleSchema);
api.js
var bodyParser = require("body-parser"); // get body-parser
var article = require("../models/article");
var config = require("../../config");
module.exports = function(app, express) {
var apiRouter = express.Router();
// Some stuff
var article = new article(); // create a new instance of the article model
// ...
The error is being thrown when the api tries to create the new Article, here is a screenshot of the full error:
The line 34:34 is when I try to initiate the new article.
I am aware this question gets asked all the time, and I'm sorry if the error is incredibly stupid but I went through 20 different "TypeError: undefined" questions, trying different things inside and I could not for the life of me fix it.
I am getting a JS TypeError: undefined is not a function
when trying to create an instance of a schema I define and import.
article.js
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
// Article Schema
var ArticleSchema = new Schema({
title : { type: String, required: true, index: { unique: true }},
author : { type: String, required: true },
day : { type: String, required: true },
month : { type: String, required: true },
year : { type: String, required: true },
link : { type: String, required: true },
password : { type: String, required: false, select: false }
});
module.exports = mongoose.model("Article", ArticleSchema);
api.js
var bodyParser = require("body-parser"); // get body-parser
var article = require("../models/article");
var config = require("../../config");
module.exports = function(app, express) {
var apiRouter = express.Router();
// Some stuff
var article = new article(); // create a new instance of the article model
// ...
The error is being thrown when the api tries to create the new Article, here is a screenshot of the full error:
The line 34:34 is when I try to initiate the new article.
I am aware this question gets asked all the time, and I'm sorry if the error is incredibly stupid but I went through 20 different "TypeError: undefined" questions, trying different things inside and I could not for the life of me fix it.
Share Improve this question asked Feb 12, 2015 at 16:13 ErickErick 2,6086 gold badges31 silver badges44 bronze badges 1-
2
try
var myArticle = new article();
– Pointy Commented Feb 12, 2015 at 16:15
2 Answers
Reset to default 3You're declaring a variable called "article". That's the same name you used for the module you imported, so your local variable will hide the more global one. Variables start off with no value, so they're undefined
.
If you change the local variable name, then assuming your export is set up correctly you will be able to access the constructor.
Use a different name: If you do var article
you overwrite the initial var article
and thus it doesn't work.
Good practice is to use uppercase for the ModelNames:
var bodyParser = require("body-parser"); // get body-parser
var Article = require("../models/article");
var config = require("../../config");
module.exports = function(app, express) {
var apiRouter = express.Router();
// Some stuff
var article = new Article(); // create a new instance of the article model
// ...
Try it like this, should work now :)