Reputation: 82590
I made an express app, and it has an app inside of that called users. I am using Typescript. This is what my app.js file looks like:
///<reference path='d.ts/DefinitelyTyped/node/node.d.ts' />
///<reference path='d.ts/DefinitelyTyped/express/express.d.ts' />
///<reference path='routes/Users.ts' />
import express = require("express");
import http = require("http");
import path = require("path");
import us = require("./routes/Users");
var app = express();
// all environments
app.set('port', 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('env', 'development');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', (req, res) => {
res.render("index", {
title: "Express Main Page"
});
});
// Users app
app.use(us.Users.users);
http.createServer(app).listen(app.get('port'), () => {
console.log('Express server listening on port ' + app.get('port'));
});
Right now, the problem is this, I am using mongoose to store some data, and I have mongodb started. Here is what the Users.ts file looks like:
/// <reference path='../d.ts/DefinitelyTyped/node/node.d.ts' />
/// <reference path='../d.ts/DefinitelyTyped/express/express.d.ts' />
/// <reference path='../d.ts/DefinitelyTyped/mongoose/mongoose.d.ts' />
import express = require("express");
import mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/SimpleBlog");
export module Users {
// Exporting information about the user class
export var users: Express = express();
export var base_URL: string = "/users";
// Making Schemas
var UserSchema = new mongoose.Schema({
email: String,
name: String,
age: Number
});
// Attaining users
var db_Users = mongoose.model('Users', UserSchema);
var list;
db_Users.find({}, (err, docs) => {
list = docs;
console.log(docs);
});
// Route for base url
users.get(base_URL, (req, res) => {
res.render("Users/index", {
title: "User List",
user_list: list
});
});
// Route for POST request
users.post(base_URL + "/add", (req, res) => {
try {
console.log(req.body['name']);
new UserSchema({
name: req.body['name'],
email: req.body['email'],
age: req.body['age']
}).save(function (err, docs) {
if (err) { console.log(err); }
});
} catch (Exception) {
console.log(Exception);
}
res.redirect(base_URL);
});
users.get(base_URL + "/add", (req, res) => {
res.render("Users/add", {});
});
}
I get [TypeError: object is not a function]
as the error, when I try to save.
The users/add
simple gives the user a registration form, and this is done in a jade file. I can also attest that it is not a problem with express.bodyParser()
since console.log(req.body['name']);
prints out the name of the post request correctly.
Any help regarding this would be helpful.
Upvotes: 2
Views: 138
Reputation: 59793
Your syntax isn't correct for Mongoose.
Regarding this line:
// Attaining users
var db_Users = mongoose.model('Users', UserSchema);
That line returns a Model
function/constructor. You'll use that result to create instances of a User
. Given all of the Users/UserSchema/User namespace you've got right now, it's a little confusing, so I'll switch it:
// Attaining users
var UserModel = mongoose.model('User', UserSchema);
It looks like the class represents a User, not Users.
When you want to create an instance of a User, you'd create an instance of the UserModel
class rather than the schema:
new UserModel({
name: req.body['name'],
email: req.body['email'],
age: req.body['age']
}).save(function (err, docs) {
if (err) { console.log(err); }
});
Upvotes: 1