Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems exporting model functions (Express and Mongoose)

I have been looking at code (https://github.com/cmarin/MongoDB-Node-Express-Blog) to learn NodeJS, Express, Mongoose, and I am having trouble importing a 'Poll' function from my 'models.js' file, particularly the 'save' function.

I am getting the following error:

500 TypeError: Object function (){} has no method 'save'

It occurs on line 54 of my app.js. I am unable to save a new Poll because it cannot find the function: https://github.com/kelper/Poll/blob/master/app.js

Here is my models file, and the save function is on line 62: https://github.com/kelper/Poll/blob/master/models.js

One other quick question. How can I exclude files from being committed? I keep committing swap files and such to my repo.

If you see anything else wrong with my code, please tell me. I know one person mentioned that my naming conventions are confusing. How should I be naming my variables?

like image 299
Kelp Avatar asked Feb 23 '26 17:02

Kelp


1 Answers

PollModel is a function constructor, you want to create an object.

var PollModel = require('./models').PollModel; is wrong

var pollModel = new (require('./models').PollModel); is right.

like image 135
Raynos Avatar answered Feb 27 '26 01:02

Raynos