最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Javascript TypeError: undefined is not a function when initializing an instance - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

You'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 :)

发布评论

评论列表(0)

  1. 暂无评论