I have a background in PHP MVC and just started learning Nodejs.
This is my app directory structure:
root
- controllers
-user.js
- model
-user.js
- public
-stylesheet
-style.css
- views
-index.jade
- app.js
- package.json
- gulpfile.js
This is my controller file: user.js
var User = require('../models/user.js');
exports.list = function(req, res){
User.find(function(err, users) {
res.send(users);
});
};
This is my model file: user.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.createConnection('mongodb://localhost/mydb');
var userSchema = new Schema({
name: String,
country: String,
email: String,
password: String,
});
module.exports = mongoose.model('user', userSchema);
Below is my app.js file:
/**
* Module dependencies.
*/
var express = require('express')
, controllers = require('./controllers')
, employee = require('./controllers/employee')
, user = require('./controllers/user')
, http = require('http')
, path = require('path');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', controllers.index);
app.get('/employees', employee.list);
app.get('/user',user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
I have created a database in mongo using the command line and below is my collections list:
I may still be thinking it the php way and missing some point here. Could someone point out what I am doing wrong here ?
Mongoose automatically looks for the plural version of your model name, more details about this rule please refer to the source code, Mongoose.model definition is
Mongoose#model(name, [schema], [collection], [skipInit])
To solve your issue, you could define your model
mongoose.model('user', userSchema, { collection: 'user' });
Related
I'm trying to learn the MEAN stack by following this tutorial and I have run into an error. Unfortunately, I can't spot out where I went wrong exactly.
I was trying to test routes out in Postman by creating a user but I kept getting back 'Cannot POST /users'.
Can anyone help me out here? Thanks in advance!
routes.js
// Dependencies
var mongoose = require('mongoose');
var User = require('./model.js');
// Opens App Routes
module.exports = function(app) {
// GET Routes
// --------------------------------------------------------
// Retrieve records for all users in the db
app.get('/users', function(req, res){
// Uses Mongoose schema to run the search (empty conditions)
var query = User.find({});
query.exec(function(err, users){
if(err)
res.send(err);
// If no errors are found, it responds with a JSON of all users
res.json(users);
});
});
// POST Routes
// --------------------------------------------------------
// Provides method for saving new users in the db
app.post('/users', function(req, res){
// Creates a new User based on the Mongoose schema and the post bo.dy
var newuser = new User(req.body);
// New User is saved in the db.
newuser.save(function(err){
if(err)
res.send(err);
// If no errors are found, it responds with a JSON of the new user
res.json(req.body);
});
});
};
model.js
// Pulls Mongoose dependency for creating schemas
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Creates a User Schema. Defines how user data is stored to db
var UserSchema = new Schema({
username : {type: String, required: true},
gender : {type: String, required: true},
age : {type: Number, required: true},
favlang : {type: String, required: true},
location : {type: [Number], required: true}, //[Long, Lat]
htmlverified : String,
created_at : {type: Date, default: Date.now},
updated_at : {type: Date, default: Date.now}
});
// Sets the created_at parameter equal to the current time
UserSchema.pre('save', function(next){
now = new Date();
this.updated_at = now;
if(!this.created_at){
this.created_at = now
}
next();
});
// Indexes this schema in 2dsphere format (critical for running proximity searches)
UserSchema.index({location: '2dsphere'});
// Exports the UserSchema for use elsewhere. Sets the MongoDB collection to be used as:
module.exports = mongoose.model('scotch-user', UserSchema);
server.js
// Dependencies
// -----------------------------------------------------
var express = require('express');
var mongoose = require('mongoose');
var port = process.env.PORT || 3000;
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
// Express Configuration
// -----------------------------------------------------
// Sets the connection to MongoDB
mongoose.connect("mongodb://localhost/MeanMapApp");
// Logging and Parsing
app.use(express.static(__dirname + '/public')); // sets the static files location to public
app.use('/bower_components', express.static(__dirname + '/bower_components')); // Use BowerComponents
app.use(morgan('dev')); // log with Morgan
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.urlencoded({extended: true})); // parse application/x-www-form-urlencoded
app.use(bodyParser.text()); // allows bodyParser to look at raw text
app.use(bodyParser.json({ type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
app.use(methodOverride());
// Routes
// ------------------------------------------------------
require('./app/routes.js')(app);
// Listen
// -------------------------------------------------------
app.listen(port);
console.log('App listening on port ' + port);
I believe this is where your error is.
Instead of:
module.exports = mongoose.model('scotch-user', UserSchema);
Try:
module.exports = mongoose.model('User', UserSchema);
Also, look into using Express.js for your routes. When you are testing on Postman, double check that you are entering all "required" parts for your MongoDB Schema.
I just copied your scripts and I have no problems at all!
Make sure you're using the right methods and routes in postman.
Small tip: Mongoose handles it own schema's, so there is no need to export them.
You can easily do the following
app.js
// Dependencies
// -----------------------------------------------------
var express = require('express');
var mongoose = require('mongoose');
var port = process.env.PORT || 3000;
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
// Load all models
require('./app/model');
// Express Configuration
model.js
// remove module.exports in the last line
mongoose.model('User', UserSchema);
routes.js
// Dependencies
var mongoose = require('mongoose');
var User = mongoose.model('User');
I'm quite new to Node, I've been following a tutorial to build a simple server that serves dynamic pags with some basic routes but keep getting the Error: Cannot GET/ after running node server.js and calling localhost:3300 on the browser. My routes are defined externally and initialized using a routes.initialise() as follows:
//routes.js
var home = require('../controllers/home'),
image = require('../controllers/image'),
express = require('express');
module.exports.initialize = function(app, router) {
//var router = express.Router();
router.get('/', home.index);
router.get('/images/:image_id', image.index);
router.post('/images', image.create);
router.post('/images/:image_id/like', image.like);
router.post('/images/:image_id/comment', image.comment);
app.use('/', router);
};
I've searched far and wide but no solution forthcoming. I'm really frustrated and will need some help here.
I have a server.js that creates the server:
//server.js
var express = require('express'),
config = require('./server/configure'),
app = express();
app.set('port', process.env.PORT || 3300);
app.set('views', __dirname + '/views');
app = config(app);
var server = app.listen(app.get('port'), function() {
console.log('Server up: http://localhost:' + app.get('port'));
});
and a configure.js that configures the server:
//configure.js
var path = require('path'),
routes = require('./routes'),
exphbs = require('express3-handlebars'),
express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler');
module.exports = function(app) {
app.engine('handlebars', exphbs.create({
defaultLayout: 'main',
layoutsDir: app.get('views') + '/layouts',
partialsDir: [app.get('views') + '/partials']
}).engine);
app.set('view engine', 'handlebars');
app.use(morgan('dev'));
app.use(bodyParser({
uploadDir:path.join(__dirname, 'public/upload/temp')
}));
app.use(methodOverride());
app.use(cookieParser('some-secret-value-here'));
routes.initialize(app, new express.Router());
app.use('/public/', express.static(path.join(__dirname, '../public')));
if ('development' === app.get('env')) {
app.use(errorHandler());
}
return app;
};
also two files, home.js and image.js which are supposed to provide the default routes from configure.js:
//home.js
module.exports = {
index: function(req, res) {
res.send('The home:index controller');
}
};
//image.js
module.exports = {
index: function(req, res) {
res.send('The image:index controller ' + req.params.image_id);
},
create: function(req, res) {
res.send('The image:create POST controller');
},
like: function(req, res) {
res.send('The image:like POST controller');
},
comment: function(req, res) {
res.send('The image:comment POST controller');
}
};
Anytime I try to GET any of the links on the browser it returns the Cannot GET/ error. Any help will be greatly appreciated.
Thank you so much :)
I actually copied your exact code and it worked as you expect, though frankly it's got a bunch of indirection and at least one deprecated module. Maybe you have a PORT environmental variable set, and so the app isn't actually running on 3300?
app.js file:
var express = require('express');
var path = require('path');
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var conn = mongoose.createConnection('mongodb://localhost/database_name');
var app = express();
app.set('view engine','ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyparser.urlencoded({extended:true}));
app.use(require('./routes/index'));
app.use(require('./routes/page1'));
app.use(require('./routes/page2'));
app.use(require('./routes/page3'));
app.use(require('./routes/page4'));
exports.conn = conn;
app.listen(3000,function(request, response){
console.log("Server is running at Port 3000");
});
models.js file:
var mongoose = require('mongoose');
var app = require('./app');
var conn = app.conn
module.exports.User = conn.model('User', new mongoose.Schema({
username: String,
password: String,
}));
On running node app.js, i am getting an error that TypeError: Cannot read property 'model' of undefined.
app.js and models.js are in the same folder.
P.S. i am using createConnection function because i have to make two database connections.
You should not require app.js inside a module. That defeats the purpose. Either use a separate module that handles the connection, or use dependency injection like this:
models.js
var mongoose = require('mongoose');
module.exports = function(conn) { // inject connection
return {
User: conn.model('User', new mongoose.Schema({
username: String,
password: String,
})
};
}
app.js
var conn = mongoose.createConnection('mongodb://localhost/database_name');
var models = require('./models')(conn);
models.User(...) // do stuff with User function
This is better structured, more maintainable and better testable as you can inject a mock for conn.
finally figured it out:
to make it globally available we need to replace following line
var conn = mongoose.createConnection('mongodb://localhost/database_name');
with
module.exports.conn = mongoose.createConnection('mongodb://localhost/database_name');
Hi i am developing nodejs application with express and mongodb.So i must define mongoose and schema in my all routing js to use mongo and schema. I want to define them only ones. I am new at node.js so please be patient. My project structure:
My route.js is shown below:
var routes = function (app) {
app.locals.error=null;
app.get('/login', function (req, res) {
return res.render(__dirname + "/views/login", {
title: 'Giriş',
stylesheet: 'login'
});
});
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/fuatblog");
var UserSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
age: Number
}),
Users = mongoose.model('Users', UserSchema);
app.post('/sessions', function (req, res) {
console.log(req.body.login.email);
console.log(req.body.login.password);
console.log(req.body.login.rememberMe);
Users.find({
email: req.body.login.email,
password: req.body.login.password
}, function (err, docs) {
if (! docs.length) {
// no results...
console.log('User Not Found');
res.status(400);
return res.render(__dirname + "/views/login", {
title: 'Giriş',
stylesheet: 'login',
error: 'Kullanıcı adı veya şifre yanlış'
});
}
console.log('User found');
req.session.email = docs[0].email;
console.log(req.session.email);
return res.redirect('/Management');
});
});
};
module.exports = routes;
And my server.js(app.js)
/**
* Module dependencies.
*/
////Mongoose eklendi
var express = require('express'),
mongoose= require('mongoose');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
////Cookie için eklendi.
app.use(express.cookieParser());
////Session desteği için eklendi
app.use(express.session({secret: 'asdfsdfsafasdfasdfasdf'}));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
////Put ve Delete mothodları için
app.use(express.methodOverride());
////Requeestleri ayrıştırmak için kullanılıyor
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
////Helpers
//require('./apps/helpers')(app);
//Routes
require('./apps/authentication/routes')(app)
require('./apps/blog/routes')(app)
require('./apps/management/routes')(app)
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
I'm going to assume that you meant that you want to define the Schema only ONCE.
What I like to do is to initialize all the models that I'm going to use when the server starts and the mongodb connection is established.
An ideal directory structure would be something like this:
mongodb
--schemas
----user.js
--models.js
You would put your schemas inside the schema folder, such as your User model:
(user.js)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = function() {
var UserSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
age: Number
});
mongoose.model("User", UserSchema);
};
In models.js, you would have code that initializes each schema model in the schemas directory.
(models.js)
exports.initialize = function() {
require("fs").readdirSync(__dirname + "/schemas").forEach(function(file) {
require('./schemas/' + file)();
});
};
In your app.js file, you would do this call to initialize all your schemas AFTER you establish your connection to mongoose:
require($pathToYourModelsJsFile).initialize();
After this, you are all set to use your models! All you have to do for when you want to use them is:
var mongoose = require('mongoose');
var User = mongoose.model('User');
// Do work
User.find();
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.
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); }
});